Php: remove a word from a sentence

i has a suggestion


something about something WORD still something...


What is the most efficient method to remove the word " WORD " from a sentence in php? thanks

+4
source share
3 answers

You can replace it with anything:

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

Although it will also destroy words like swordfish , turning them into sfish . This way you can put spaces around the edges:

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

But then it will not correspond to the words at the end and beginning of sentences. Therefore, you may need a regular expression:

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

\b is the word boundary, which can be a space or the beginning of a line, or something like that.

+14
source

Try the following:

 $fixed_string = str_replace(" WORD ", " ", $your_string); 
+2
source

Depends, str_replace may be what you are looking for. But keep in mind that it removes all occurrences.

+1
source

Source: https://habr.com/ru/post/1307194/


All Articles