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


How do I remove the apostrophe before inserting into MYSQL? (not simply escaping them)

我有一个程序可以获取有时带有撇号的网页标题。 我不只是想将它们注释掉并用''s转义它们。 我怎样才能一起摆脱它们?

str_replace()应该可以工作:

$string = str_replace("'", "", $string);
您可以使用

此功能清除任何不需要的字符中的标题。

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.
$string = str_replace("'", "'", $string);

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

如果要

插入包含撇号的字符串,请在代码中添加以下行以转义它们:

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

如果您尝试在MySQL更新之前进行修剪,或插入...

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

很好,但 MySQL 可以使用另一个符号而不是修剪后的符号。

在这里,我开发的是在回显 mysql 值后进行修剪。从 mysql 读取:

$result = mysqli_query($conn, "SELECT value1 FROM db WHERE id='1'  ");
if (mysqli_num_rows($result) > 0) {while($row = mysqli_fetch_assoc($result)) {
echo str_replace('"', '', $row["value"]);
}
}