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

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

报告相同问题?