質問
カーリーアポストロフィ(ある種のリッチテキストドキュメントから貼り付けられたもの)を削除しようとしていますが、障害になっているようです。以下のコードは私には機能しません。
$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配列から削除すると、置換が行われますが、アンパサンドが文字列に残っているため、これは明らかに仕事を完了しません。アンパサンドに遭遇したときにstr_replaceが失敗するのはなぜですか?
解決
これを行うだけではありません:
$word = htmlentities(str_replace($search, $replace, $word), ENT_QUOTES);
?
他のヒント
物事を適切に機能させるために、@ cletusがレイアウトした例よりも少し堅牢なものが必要でした。ここに私のために働いたものがあります:
// String full of rich characters
$string = 物事を適切に機能させるために、@ cletusがレイアウトした例よりも少し堅牢なものが必要でした。ここに私のために働いたものがあります:
<*>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);
所属していません StackOverflow