如何清理包含&符号的文本字符串以便与另一个文本字符串进行比较
因为有多种编码“特殊”字符的方法,特别是与符号,所以如何进行字符串比较,从针和大海捞针中删除所有特殊字符,以允许“苹果对苹果”比较来检查大海捞针出现?
例如,如果我有一根针“black&decer”,并且我想将其消毒为“black decker”,然后查看“black decker”是否出现在干草堆中,我将需要进行与我所做的相同的更换从针到干草堆,以便解释对&符号进行编码的所有方式以及如何对“black & decker”进行编码以出现在干草堆中。
&
& (I've only seen this in WordPress editor markup)
&
是否有 preg_replace、正则表达式或替换方法可以在一定程度上准确地做到这一点?
Because there are multiple ways of encoding "special" characters, in particular the ampersand, how would one do a string comparison that removes all special characters from both the needle and the haystack to allow for an "apples to apples" comparison to check that the needle appears in the haystack?
For example, if I have a needle "black & decker", and I want to sanitize it down to "black decker" and then see if "black decker" appears in the haystack, I will need to do the same replacement I did on needle to haystack in order to account for all ways of encoding the ampersand and how "black & decker" might be encoded to appear in the haystack.
&
& (I've only seen this in WordPress editor markup)
&
Is there a preg_replace, regex or replacement method that can do this with some degree of accuracy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想将字符串减少为仅字母、数字和空格吗?为此我会用
preg_replace('/[^\w\d]/', '')
这基本上消除了任何不是“单词字符”、数字或空格的内容
do you want to reduce the string down to just letters, numbers and spaces? For that I'd use
preg_replace('/[^\w\d ]/', '')
which basically eliminates anything that's not a "word character", digit, or space