PHP preg_replace 从子字符串和结束标记中删除文本?

PHP preg_replace remove text from substring and end tag?

我有这个字符串 (html),想删除从“.cf”到段落结尾的所有内容。

$html = <p>text to keep .cf text to remove </p>

所以,都在 .cf 之间。 (包括 .cf)和

应该被删除以具有:

$html = <p>text to keep</p>

我尝试了几种方法但找不到解决方案(认为我混合了空格和点)

$html = preg_replace('/ \.cf (.*?)<\/p>/i','</p>',$html);

$html = preg_replace('#{{ \.cf (.*?)</p>}}#s','</p>',$html);

这应该可以解决问题。

$html = '<p>text to keep .cf text to remove </p>';
$html = preg_replace('/\s+\.cf\s*(.*?)<\/p>/i','</p>',$html);
var_dump($html);

\s代表白色space char.

+表示一个或多个.

*表示零个或多个.

只是为了更清楚。

\s+\.cf\s*表示会有one or more white space char before .cf and zero or more white space char after .cf