简体   繁体   中英

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...
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. It's a Unicode special character ( U+FFFD ). The following code will preserve letters, numbers, punctuation marks and whitespaces, at the same time removing all other Unicode characters:

$refine_re = preg_replace("/[^[:alnum:][:punct:][:space:]]/u", "", $refine_re);
print($refine_re);

Look at the u modifier at the end of the regex. You need it because the string has Unicode special characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM