PHP正则表达式忽略特殊字符

With the help of SO, I was able to make a regular expression for my purposes, it works great, but it completely ignores special characters.

$pattern='/(?=.*\b\Q'.str_replace(' ','\E\b)(?=.*\b\Q',$requestedservice).'\E\b)/i';  
preg_match($pattern, $item)

Here $requestedservice is the character that it's trying to match with $item from the database.

The $item is Walk - Dance so if the $requestedservice is Walk - Dance as well, it's not matched, but if the $requestedservice is Walk Dance it is matched.

I am not sure why it's ignoring special characters like - / %

I am using html_entity_decode for the $requestedservice so that's not an issue.

Any guidance would be really helpful.

Your word boundaries are working against you. If you have ., for instance, your pattern is /(?=.*\b\Q.\E\b)/i, which asserts that there is a literal . with a word boundary before and after it, and since . is a non-word character, that means there has to be a word character before and after it.

Instead you could use (?<!\w) in place of the first and third \b and (?!\w) in place of the second and fourth \b to specifically assert there is not a word character before and after each of your string parts that need to match.