我想从字符串中删除特殊字符。我使用了 esc_html()
、htmlentities()
等...
我无法得到结果。
我还使用了正则表达式,如下所示:
$refine_re="My response to this article:�http://www.theaustralian.com.au/australian-it/it-business/eugene-kaspersky-queries-breach-rules/story-e6frganx-1226656443211"
preg_replace("/&(?:[a-z0-9;]{2,8}|#[0-9;]{2,4})/i", '', $refine_re);
最佳答案
您尝试删除的字符根本不是 HTML 实体。它是一个 Unicode 特殊字符 ( U+FFFD )。以下代码将保留字母、数字、标点符号和空格,同时删除所有其他 Unicode 字符:
$refine_re = preg_replace("/[^[:alnum:][:punct:][:space:]]/u", "", $refine_re);
print($refine_re);
查看正则表达式末尾的 u
修饰符。您需要它是因为该字符串具有 Unicode 特殊字符。
关于javascript - 如何在 WordPress、PHP、JavaScript 上使用正则表达式从字符串(URL)中删除特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21227235/