2017-08-08 15 views
3

<p></p>以外のすべての<br>タグを削除します。しかし、<p></p>内の休憩は傷ついてはいけません。どのように私はこれをPHPで達成することができます。以下は例です。段落の外側から区切りタグを削除する方法

$html = "<br> <p> This is the Firs para <br> 
     This is a line after the first break <br> 
     This is the line after the 2nd break <br> 
     Here the first para ends </p> 
     <br> 
     <br> 
     <p> This is the 2nd para <br> 
     This is a line after the first break in 2nd para <br> 
     This is the line after the 2nd break <br> 
     Here the 2nd para ends </p>" 

私は結果が

以下
$html = "<p> This is the Firs para <br> 
     This is a line after the first break <br> 
     This is the line after the 2nd break <br> 
     Here the first para ends </p> 
     <p> This is the 2nd para <br> 
     This is a line after the first break in 2nd para <br> 
     This is the line after the 2nd break <br> 
     Here the 2nd para ends </p>" 

答えて

4

これはあなたを助けるでしょう。

$out = preg_replace("(<p(?:\s+\w+(?:=\w+|\"[^\"]+\"|'[^']+')?)*>.*?</p>(*SKIP)(*FAIL)" 
."|<br>)is", "", $html); 

echo $out; 
+0

これはうまくいきましたが、私は小さな変更を加えなければなりませんでした。私は正規表現の中に一重引用符を入れ、すべての二重引用符を削除しなければならなかった。ありがとう@Arturs – Jeeva

0

以下のようになりたいのソリューションは、それはあなたを助けることができるかもしれです。

$html = ""; 
$html .="<p><br>This is the Firs para <br> 
     This is a line after the first break <br> 
     This is the line after the 2nd break <br> 
     Here the first para ends </p>"; 

     $html .= "<p><br> This is the 2nd para <br> 
     This is a line after the first break in 2nd para <br> 
     This is the line after the 2nd break <br> 
     Here the 2nd para ends </p>"; 

echo $html; 
1
<?php 
$text = '<br> <p> This is the Firs para <br> 
     This is a line after the first break <br> 
     This is the line after the 2nd break <br> 
     Here the first para ends </p> 
     <br> 
     <br> 
     <p> This is the 2nd para <br> 
     This is a line after the first break in 2nd para <br> 
     This is the line after the 2nd break <br> 
     Here the 2nd para ends </p>'; 

$pattern = '/(<br>[\s\r\n]*<p>|<\/p>[\s\r\n]*<br>)/'; 
$replacewith = '<p>'; 
echo preg_replace($pattern, $replacewith, $text); 
関連する問題