如何清理包含&符号的文本字符串以便与另一个文本字符串进行比较

发布于 2024-11-30 05:36:35 字数 407 浏览 0 评论 0原文

因为有多种编码“特殊”字符的方法,特别是与符号,所以如何进行字符串比较,从针和大海捞针中删除所有特殊字符,以允许“苹果对苹果”比较来检查大海捞针出现?

例如,如果我有一根针“black&decer”,并且我想将其消毒为“black decker”,然后查看“black decker”是否出现在干草堆中,我将需要进行与我所做的相同的更换从针到干草堆,以便解释对&符号进行编码的所有方式以及如何对“black & decker”进行编码以出现在干草堆中。

&
& (I've only seen this in WordPress editor markup)
&#038

是否有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2024-12-07 05:36:35

您想将字符串减少为仅字母、数字和空格吗?为此我会用
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文