Ant*_*ony 7 php string escaping
我正试图摆脱卷曲的撇号(我想象的那些粘贴在某种富文本文档中),我似乎正在遇到路障.以下代码对我不起作用.
$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'.
Run Code Online (Sandbox Code Playgroud)
当我从$ search数组中删除&符号时,会发生替换,但显然,由于&符号保留在字符串中,因此显然无法完成工作.为什么str_replace在遇到&符时失败了?
cle*_*tus 11
为什么不这样做:
$word = htmlentities(str_replace($search, $replace, $word), ENT_QUOTES);
Run Code Online (Sandbox Code Playgroud)
?
小智 6
为了让我能够正常工作,我需要比@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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7523 次 |
| 最近记录: |