2017-06-12 148 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>