PHP-去掉卷曲撇号
我正试图摆脱卷曲撇号(我想是从某种富格文本文档中粘贴的撇号),我似乎遇到了一个障碍。下面的代码对我不起作用PHP-去掉卷曲撇号,php,string,escaping,Php,String,Escaping,我正试图摆脱卷曲撇号(我想是从某种富格文本文档中粘贴的撇号),我似乎遇到了一个障碍。下面的代码对我不起作用 $word = "Today’s"; $search = array('„', '“', '’'); $replace = array('"', '"', "'"); $word = str_replace($search, $replace, htmlentities($word, ENT_QUOTES)); What I en
$word = "Today’s";
$search = array('„', '“', '’');
$replace = array('"', '"', "'");
$word = str_replace($search, $replace, htmlentities($word, ENT_QUOTES));
What I end up with is $word containing 'Today’s'.
当我从$search数组中删除符号AND时,会进行替换,但这显然无法完成任务,因为符号留在字符串中。为什么str_replace遇到符号时会失败?为什么不这样做:
$word = htmlentities(str_replace($search, $replace, $word), ENT_QUOTES);
?为了让事情正常运行,我需要比@cletus的例子更健壮的东西。以下是对我有效的方法:
// String full of rich characters
$string = $_POST['annoying_characters'];
// Replace "rich" entities with standard text ones
$search = array(
'“', // 1. Left Double Quotation Mark “
'”', // 2. Right Double Quotation Mark ”
'‘', // 3. Left Single Quotation Mark ‘
'’', // 4. Right Single Quotation Mark ’
''', // 5. Normal Single Quotation Mark '
'&', // 6. Ampersand &
'"', // 7. Normal Double Qoute
'<', // 8. Less Than <
'>' // 9. Greater Than >
);
$replace = array(
'"', // 1
'"', // 2
"'", // 3
"'", // 4
"'", // 5
"'", // 6
'"', // 7
"<", // 8
">" // 9
);
// Fix the String
$fixed_string = htmlspecialchars($string, ENT_QUOTES);
$fixed_string = str_replace($search, $replace, $fixed_string);
//充满丰富字符的字符串
$string=$\u POST['讨厌的字符'];
//将“丰富”实体替换为标准文本实体
$search=array(
““;”,//1.左双引号”
“”;”,//2.右双引号”
“‘;”,//3.左单引号”
“’;”,//4.右单引号”
“';”,//5.普通单引号”
“&;”,//6.与符号&
“,//7.正规双Qoute
'',//8.小于<
''//9.大于>
);
$replace=数组(
'"', // 1
'"', // 2
"'", // 3
"'", // 4
"'", // 5
"'", // 6
'"', // 7
"" // 9
);
//修理绳子
$fixed_string=htmlspecialchars($string,ENT_引号);
$fixed_string=str_replace($search,$replace,$fixed_string);
那些卷曲的撇号被称为智能引号。哇,这太简单了。我想我写代码太久了!