Syom Syom - 8 months ago 55
PHP Question

php: delete word from sentence

i have the sentence

something about something
still something...

what is the most efficient metod to delete the word "WORD" from sentence in php?


You could replace it with nothing:

$sentence = str_replace('word', '', $sentence);

Although that would also ruin words like swordfish, turning them into sfish. So you could put spaces around the edges:

$sentence = str_replace(' word ', ' ', $sentence);

But then it won't match words at the end and beginning of sentences. So you might have to use a regex:

$sentence = preg_replace('/\bword\b/', '', $sentence);

The \b is a word boundary, which could be a space or a beginning of a string or anything like that.