dongwei3172 2011-08-17 16:21
浏览 43
已采纳

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

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)
&#038

Is there a preg_replace, regex or replacement method that can do this with some degree of accuracy?

  • 写回答

1条回答 默认 最新

  • doulao1966 2011-08-17 16:31
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据