用于切割字符串的正则表达式

regex for cutting a string

我正在 php 中寻找正则表达式,它在第一个 (<br>|<br />|<p>)

处截断一个字符串

我得到了类似的东西,但它仍然无法正常工作

$newstring = preg_replace( '#< /?\s*(br|p) >.*$#i', '', $string );

例如,如果 $string 是:

<b>this is a test</b><bR>and it is going on<br /> and so on<p>

预计$newstring

<b>This is s test</b>

您为什么喜欢正则表达式?为什么不

$newstring = preg_replace('~(<br ?/?>|<p>).*~i', '', $string );

参见 live demo 个:

$string = '<b>this is a test</b><bR>and it is going on<br /> and so on<p>';
$newstring = preg_replace('~(<br ?/?>|<p>).*~i', '', $string );
echo $newstring;

输出:

<b>this is a test</b>