我正试图摆脱卷曲的撇号(从我想象的某种富文本文档粘贴的那些)我似乎正在遇到路障。以下代码对我不起作用。

$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( '&#8220;', // 1. Left Double Quotation Mark “ '&#8221;', // 2. Right Double Quotation Mark ” '&#8216;', // 3. Left Single Quotation Mark ‘ '&#8217;', // 4. Right Single Quotation Mark ’ '&#039;', // 5. Normal Single Quotation Mark ' '&amp;', // 6. Ampersand & '&quot;', // 7. Normal Double Qoute '&lt;', // 8. Less Than < '&gt;' // 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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top