使用php仅删除第一行的word
我有文字:使用php仅删除第一行的word,php,Php,我有文字: I have new blue car I have new red and blue cars 如何使用php从第一行删除我想要的单词 例: 结果应该是: I have new car I have new red and blue cars 我想举一个例子,说明删除p br是可能的 <p></p><br/>I have new blue car I have new red and blue cars 以下内容将找到第一行,将该行上的“蓝
I have new blue car
I have new red and blue cars
如何使用php从第一行删除我想要的单词
例:
结果应该是:
I have new car
I have new red and blue cars
我想举一个例子,说明删除p br是可能的
<p></p><br/>I have new blue car
I have new red and blue cars
以下内容将找到第一行,将该行上的“蓝色”字替换为零删除它,去掉标记并删除前导/尾随空格 只会删除整个单词,例如“blues”中的“not'blue” 如果在第一行中找不到单词,则不会从以下行中删除该单词 不会从以下行中删除标记 代码: 输出:
I have new car
I have new <b>red<b> and blue cars
如何仅用于第一行?文本较长,包含更多标记。在textarea中输入文本,并需要在前线编辑标记
$text = "<p></p><br/>I have new blue car
I have new <b>red<b> and blue cars";
$word = 'blue';
$text = preg_replace_callback(
'/.*$/m', // Match single line
function ($matches) use ($word) {
// Remove word (\b = word boundary), strip tags and trim off whitespace
return trim(
strip_tags(
preg_replace('/\b' . $word. '\s*\b/', '', $matches[0])
)
);
},
$text,
1 // Match first line only
);
echo $text, PHP_EOL;
I have new car
I have new <b>red<b> and blue cars