如何在插入MYSQL之前删除撇号? (不是简单地逃避它们)

时间:2012-05-04 01:20:07

标签: php mysql

我有一个程序可以获取有时会有撇号的网页标题。我不只是想评论它们并用\ s逃脱它们。我怎么能一起摆脱它们?

4 个答案:

答案 0 :(得分:16)

str_replace()应该有效:

$string = str_replace("'", "", $string);

答案 1 :(得分:1)

您可以使用此功能清除任何不需要的字符。

function clean_up( $text ) {
    $unwanted = array("'"); // add any unwanted char to this array
    return str_ireplace($unwanted, '', $text);
    }

使用它像:

$dirty_title = "I don't need apostrophes.";
$clean_title = clean_up($dirty_title);
echo $clean_title; // outputs: I dont need apostrophes.

答案 2 :(得分:1)

$string = str_replace("'", "'", $string);

这种方法是PHP& MySQL安全,并且在回显时不会改变字符串的外观。在处理像奥布莱恩这样的名字时很有用。

答案 3 :(得分:0)

如果要插入包含撇号的字符串,请在代码中添加以下行以逃避它们:

$data = str_replace("'", "\'", $data);