开发者

How to sanitize a string of text containing ampersands for comparison to another string of text

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜