开发者

php: delete word from sentence

i have the sentence


something about something WORD still something...


what is the most efficient metod to delete the word "WORD" from sentence in php开发者_开发问答? thanks


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.


Depends, str_replace might be what you're looking for. But note that it removes all occurrences.


Try this:

$fixed_string = str_replace(" WORD ", "  ", $your_string);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜