I hav especific need of removing word from string, But I am having problem when that word has dot (.) character.
Lets see here is the string and what I have tried so far?
$result = 'Hello Ka Kashish.';
$result = preg_replace('/\bKa\b/i', '', $result);
I will get the expected result 'Hello Kashish.'
But if the string is like below, It is not working
$result = 'Hello Ka. Kashish.';
$result = preg_replace('/\bKa.\b/i', '', $result);
It gives me result 'Hello Ka. Kashish.'
Why this .(dot) is not working?
Please give me solution.
And if I can achive this word removal in any other way, pLease let me know.
I want to remove only word not set of charaters, as ‘Ka’ word will be removed, but ‘Ka’ will not be removed from ‘Kashish’.
Please help me.
Thanks in Advance
This is because the dot can match any character.
The problem, too, is that
\breally matches a word frontier, ie a position where a word character is followed by a non word character, or a non word character is followed by a word character. But as a dot is not a word character and neither is a space for that matter, it won’t match.Maybe you should try that instead: