我知道如何在php中用空格替换字符串中的撇号。 但是,如何在数组项中删除Apostrophes?
//Example replacing Apostrophe with space
$string = str_replace("'", "", $escapestring);
if ( $f_pointer === false )
die ("invalid URL");
$ar=fgetcsv($f_pointer);
while(! feof($f_pointer))
{
$ar=fgetcsv($f_pointer);
//DO REPLACE HERE BEFORE INSERTION INTO SQL STATEMENT
$sql="INSERT INTO x
(1,2,3,4,5,6)
values('$array[0]','$array[1]','$array[2]',
'array[3]','array[4]','array[5]')";
答案 0 :(得分:1)
更新回答:
您想删除查询的撇号。这是一种使用mysqli_real_escape_string函数的更好方法:
"(PHP 5) mysqli :: real_escape_string - mysqli_real_escape_string - 在SQL语句中使用的字符串中转义特殊字符,同时考虑连接的当前字符集" | http://php.net/manual/en/mysqli.real-escape-string.php
所以你直接保护你的查询输入免受sql注入。请参阅以下示例:
$itemsToClean = ["Value with '", "Second value with '"];
// With older PHP version, you cannot use [], but use array() instead
$cleanItems = array_map('mysqli_real_escape_string', $itemsToClean);
print_r($cleanItems);
答案 1 :(得分:1)
将str_replace与数组一起用作主题
$itemsToClean = ["Value with '", "Second value with '"];
$itemsToClean = str_replace("'", '', $itemsToClean);
print_r($itemsToClean);
结果
Array
(
[0] => Value with
[1] => Second value with
)