可能性の重複を置き換える:
PHP - remove <img> tag from string文字列から画像タグを削除し、文字列で
は、私は、文字列から画像タグを削除すると同時に、何かでそれを交換する必要があります。ここで私が持っているものです。
$body = '<p>Lorem ipsum dolor sit amet:</p>
<p><img class="news" id="" src="images/news_48.png" alt="" /></p>
<p>Curabitur tincidunt vehicula mauris, nec facilisis nisl ultrices sit amet:</p>
<p><img class="gallery" id="26" src="images/gallery_48.png" alt="" /></p>
<p><img id="this_image_must_stay" src="images/some_image.png" alt="" /></p>';
それは=「ギャラリー」クラスの場合、画像は、クラス=「ニュース」と別のものを持っている場合、私は一つのことを行う必要があります。 $body
今では
<?php
if(news){
replace the image tag where class=news with %%news%%
}
if(gallery){
replace the image tag where class=gallery with %%gallery%%
assign the value 26 to some variable
}
?>
含まれます:私は次のようにいくつかの擬似コードを考えている
$body = '<p>Lorem ipsum dolor sit amet:</p>
<p>%%news%%</p>
<p>Curabitur tincidunt vehicula mauris, nec facilisis nisl ultrices sit amet:</p>
<p>%%gallery%%</p>
<p><img id="this_image_must_stay" src="images/some_image.png" alt="" /></p>';
を私は/置換するpreg_matchを使用する必要が推測するが、私は正規表現が得意ではないんです。どんな助けもありがとうございます。
htmlで正規表現を使用するたびに、子猫が踏みつぶされます。 –
@MarkB http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tagsこれは(そうでない場合)このウェブサイトで最も一般的な回答の1つです。 – annonymously
またはhttp://stackoverflow.com/questions/1107194/php-remove-img-tag-from-string – krlmlr