2017-06-12 151 views
-1

我找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> 
+0

提供一个样本输入端和所希望的输出是非常赞赏。 – revo

回答

1

你为什么会喜欢这个正则表达式?为什么不只是

$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>