[英]How to remove special characters from a string (URL) using regular expression on wordpress, php, javascript
I want to remove the special char from the string. 我想从字符串中删除特殊字符。 I used
esc_html()
, htmlentities()
etc... 我用
esc_html()
, htmlentities()
等...
I can't the get the result. 我无法得到结果。
I also used regular expression, like below: 我还使用了正则表达式,如下所示:
$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);
The character you're trying to remove is not an HTML Entity at all. 您要删除的字符根本不是HTML实体。 It's a Unicode special character ( U+FFFD ).
这是Unicode特殊字符( U + FFFD )。 The following code will preserve letters, numbers, punctuation marks and whitespaces, at the same time removing all other Unicode characters:
以下代码将保留字母,数字,标点符号和空格,同时删除所有其他Unicode字符:
$refine_re = preg_replace("/[^[:alnum:][:punct:][:space:]]/u", "", $refine_re);
print($refine_re);
Look at the u
modifier at the end of the regex. 查看正则表达式末尾的
u
修饰符。 You need it because the string has Unicode special characters. 您需要它,因为该字符串具有Unicode特殊字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.