2016-07-20 4 views
1

イムでほんの数例では、いくつかのHTMLタグを削除し、私はこのから<p class="xxx"></p>タグは、PHPを使用してPHP

を削除する方法を知りたい:これまで

<p class="xxx"> 
    <a href="xxx" target="xxx"> 
     <figure> 
      <img src="xxx"/> 
      <figcaption class="xxx"> 
       <h1 class="xxx">Text</h1> 
       <cite class="xxx">Text</cite> 
      </figcaption> 
     </figure> 
    </a> 
</p> 

<a href="xxx" target="xxx"> 
    <figure> 
     <img src="xxx"/> 
     <figcaption class="xxx"> 
      <h1 class="xxx">Text</h1> 
      <cite class="xxx">Text</cite> 
     </figcaption> 
    </figure> 
</a> 

私は<p></p>を削除したいときは<p><a><figure><img/><figcaption><h1></h1><cite></cite></figcaption></figure></a></p>

私はこれを試してみる:

$html = preg_replace("' 
(<p[^>]*>)([^<]*<a[^>]*>[^<]*<figure[^>]*>[^<]*<img[^>]*>[^<]*<figcaption[^>]*>[^<]*<h1[^>]*>[^<]*</h1[^>]*>[^<]*<cite[^>]*>[^<]*</cite[^>]*>[^<]*</figcaption>[^<]*[^<]*</figure>[^<]*[^<]*</a>[^<]*)(</p>)'sim", "$2", $valBody); 
echo '<H1>Nuevo </H1><br>' . $html; 

しかし、私はそれを得る、あなたが私を助けてくれますか?

+3

regexesは使用しないでください。 [DOM](http://php.net/dom)を使用してください。あなたは、より多くの時間を欲求不満で叫んで、DOMを学ぶよりも正規表現を理解しようとしている髪を引き裂いて、あなたの声帯と髪型を元通りに保ちます。 –

+0

義務:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#answer-1732454 –

答えて

0

あなたはpreg_replace()でそれをしたい場合は、ここで私はそれをやった方法です:

<?php 

$html =<<<HTML 
<p class="xxx"> 
    <a href="xxx" target="xxx"> 
     <figure> 
      <img src="xxx"/> 
      <figcaption class="xxx"> 
       <h1 class="xxx">Text</h1> 
       <cite class="xxx">Text</cite> 
      </figcaption> 
     </figure> 
    </a> 
</p> 
HTML; 

$pattern = '#(<p.+">)#'; 
$replace = ''; 
$html = preg_replace($pattern, $replace, $html); 
$pattern = "#(</p>)#"; 
$replace = ''; 
$html = preg_replace($pattern, $replace, $html); 
$pattern = "#(\s\s)+#"; 
$replace = ''; 
$html = preg_replace($pattern, $replace, $html); 

echo '<H1>Nuevo </H1><br>' . $html; 

?> 

このコードは、それが頼まれたとしてなどの出力を与える:

<H1>Nuevo </H1><br><a href="xxx" target="xxx"><figure><img src="xxx"/><figcaption class="xxx"><h1 class="xxx">Text</h1><cite class="xxx">Text</cite></figcaption></figure></a> 

アイム初心者プログラマーとこれはスタックオーバーフローに関する私の最初の答えです。宜しくお願いします。

+0

そのokですが、すべてを削除します

タグ、私はちょうど

skycomputer2

+0

ああ:私はタグのこのシーケンスを見つけたときにそれらを削除します。状況に応じて、XPathでp要素を取得することができます(XPathを前に知ることができる場合)。その後、preg_replace()で行ったのと同様のことができます。 –

関連する問題