Question

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

Was it helpful?

Solution

This is because the dot can match any character.

The problem, too, is that \b really 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:

preg_replace('/\bKa(\W|$)/i', '', $result)

OTHER TIPS

The reason is that \b represents a word boundary. I.e. a boundary between a word character and a non-word character. See http://www.regular-expressions.info/wordboundaries.html

The boundary between a full stop "." and a space " " is not a word boundary, so the pattern match fails. Neither "." nor a back-slashed "." will work. You need to remove the second "\b".

Separately, "." means "any character", so the purpose of using back-slash "." is to ensure it matches only a full-stop, as others have pointed out. This is important to note when re-designing your pattern to work without the second "\b".

You need to escape the dot i.e. . instead of .

preg_replace('/\bKa\.\b/i', '', $result); 

perhaps this will work the way you want it to?

preg_replace('/\bKa[\.]?(\s|$)/i', '', $result);

Here is a lookahead based regex that will work for your case:

$result = 'Ka. Hello Ka. Kashish. Ka.';
$result = preg_replace('/(?<=\b)Ka\.(?=(\W|$))/i', '', $result);

OUTPUT:

string(17) " Hello  Kashish. "

rtrim is used to remove selected characters from right side of the string.

Here is an example of how to remove dot from the end of the sentence:

$result1=rtrim($result, '.');
echo $result1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top