简体   繁体   English

如何在Wordpress,PHP,JavaScript上使用正则表达式从字符串(URL)中删除特殊字符

[英]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.

相关问题 JavaScript正则表达式替换字符串中的转义特殊字符 - JavaScript Regular Expression to Replace Escape Special Characters from String 如何使用 JavaScript 中的正则表达式从字符串 (URL) 中删除 unicode 特殊字符 - How to remove unicode special characters from a string (URL) using regex in JavaScript 如何使用正则表达式删除带有数字和特殊字符的字符串 - How to remove strings with numbers and special characters using regular expression 如何使用Javascript从字符串中删除特殊字符 - How to remove special characters from a string using Javascript 如何使用JavaScript从字符串中删除特殊字符 - How to remove special characters from string using javascript 如何使用JavaScript从字符串中删除特殊字符 - How to remove the special characters from a string using javascript 使用javascript删除特殊字符的正则表达式 - Regular expression using javascript for removing the special characters JavaScript:如何使用正则表达式从URL中提取字符串 - JavaScript: How to pull out a string from a URL using a regular expression Javascript正则表达式,用于删除字符串末尾的字符 - Javascript regular expression to remove characters from the end of a string 如何使用javascript正则表达式删除带有数字和特殊字符的字符串? - How to remove strings with numbers and special characters using javascript regular expressions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM