This question already has an answer here:
I want to match one word which is not surrounded by any letter
, number
, special character
etc. Example of what I want:
Word is: Hello
Regex example I using : /\bHello\b/i
PHP code where I want to use that regex :
$text = preg_replace('/\bHello\b/i',"some img url",$text);
Result :
hello # match *(that's ok)*
hellos # no match *(that's ok)*
hello3 # no match *(that's ok)*
1hello # no match *(that's ok)*
shello # no match *(that's ok)*
$hello# # match *(that's not ok. I don't want matching!)*
hello# # match *(that's not ok. I don't want matching!)*
I want to match only if word isn't surrounded by any other letter
, number
or SPECIAL CHARACTER
.
Word boundary is ok for letters and numbers but WordB doesnt apply for special characters and I want something like WordB. that apply for special characters too...
I tried this "/(^|\s)Hello($|\s)/i"
but that's not it..
preg_replace('~(?<![\w\p{S}\p{P}])hello(?![\w\p{P}\p{S}])~i', "something", $text)
, or maybe yes, soemthing like you did but with lookarounds -"/(?<=^|\s)Hello(?=$|\s)/i"
– Wiktor Stribiżew Sep 23 '16 at 9:23/(?<!\S)hello(?!\S)/i
? – revo Sep 23 '16 at 9:24