2016-10-24 26 views
1

imageタグの先頭と最後に図形タグを追加したいとします。<figure>タグの先頭と末尾が<img>のタグ

これまで私がこれまでに試したことは次のとおりです。

<?php 
    $v['content_en'] = '<p><br /><img alt="" src="https://66.media.tumblr.com/9dc27741114bd854f29fc65dc3321958/tumblr_o9zo20vMVP1uom2zvo1_1280.png"/><br /><br /><br />The Italian-born gallerist took to the art world like some take to religion. He can be accountable for launching the careers of Rauschenberg, Twombly, Lichtenstein, Oldenburg, Stella, Judd, Flavin, Serra, and ensuring that their work was bought by the world&rsquo;s most prestigious museums.<br /> He prefered to sell to informed and engaged collectors and in all cases to philanthropists rather than speculators.<br /><br /><a href="http://www.wsj.com/articles/the-art-of-larry-gagosians-empire-1461677075" target="_blank">billion dollar annual revenue&nbsp;</a>&nbsp;are the perfect example.<br /><br />With not much space left for philosophy and introspection, today artists have to be most of all &nbsp;good communicators to get an exhibition&hellip;</p><p><br />'; 

    //$tmp= preg_replace('#<img(.*)/>(.+)#i', '<figure><img$1/></figure>', $v['content_en'], -1); 
    $v['content_en'] = preg_replace('#<img(.*)/>(.+)#i', '<figure><img$1>$2</figure>', $v['content_en'], -1); 
    //$returnValue = preg_replace('#<img(.*)>(.+)</img>#i', '<figure><img$1>$2</img></figure>', $v['content_en'], -1); 
    print_r($v['content_en']); 
?> 

$v['content_en']は、各レコードのデータベースから取得する文字列です。
私のコンテンツの最後に</figure>が追加されますが、終了するのは、<imp>タグが終了した後でなければなりません。私が正しくあなたを理解している場合

答えて

1

は、この方法を試してください。

$v['content_en'] = '<p><br /><img alt="" src="https://66.media.tumblr.com/9dc27741114bd854f29fc65dc3321958/tumblr_o9zo20vMVP1uom2zvo1_1280.png"/><br /><br /><br />The Italian-born gallerist took to the art world like some take to religion. He can be accountable for launching the careers of Rauschenberg, Twombly, Lichtenstein, Oldenburg, Stella, Judd, Flavin, Serra, and ensuring that their work was bought by the world&rsquo;s most prestigious museums.<br /><img alt="" src="https://66.media.tumblr.com/9dc27741114bd854f29fc65dc3321958/tumblr_o9zo20vMVP1uom2zvo1_1280.png"/> He prefered to sell to informed and engaged collectors and in all cases to philanthropists rather than speculators.<br /><br /><a href="http://www.wsj.com/articles/the-art-of-larry-gagosians-empire-1461677075" target="_blank">billion dollar annual revenue&nbsp;</a>&nbsp;are the perfect example.<br /><br />With not much space left for philosophy and introspection, today artists have to be most of all &nbsp;good communicators to get an exhibition&hellip;</p><p><br /><img alt="" src="https://66.media.tumblr.com/9dc27741114bd854f29fc65dc3321958/tumblr_o9zo20vMVP1uom2zvo1_1280.png"/>'; 
$returnValue = preg_replace('#<img(.*)\s?/>#iU', '<figure><img$1 $2></figure>', $v['content_en'], -1); 
echo $returnValue; 

結果は次のようになります。

<p><br /><figure><img alt="" src="https://66.media.tumblr.com/9dc27741114bd854f29fc65dc3321958/tumblr_o9zo20vMVP1uom2zvo1_1280.png" ></figure><br /><br /><br />The Italian-born gallerist took to the art world like some take to religion. He can be accountable for launching the careers of Rauschenberg, Twombly, Lichtenstein, Oldenburg, Stella, Judd, Flavin, Serra, and ensuring that their work was bought by the world&rsquo;s most prestigious museums.<br /><figure><img alt="" src="https://66.media.tumblr.com/9dc27741114bd854f29fc65dc3321958/tumblr_o9zo20vMVP1uom2zvo1_1280.png" ></figure> He prefered to sell to informed and engaged collectors and in all cases to philanthropists rather than speculators.<br /><br /><a href="http://www.wsj.com/articles/the-art-of-larry-gagosians-empire-1461677075" target="_blank">billion dollar annual revenue&nbsp;</a>&nbsp;are the perfect example.<br /><br />With not much space left for philosophy and introspection, today artists have to be most of all &nbsp;good communicators to get an exhibition&hellip;</p><p><br /><figure><img alt="" src="https://66.media.tumblr.com/9dc27741114bd854f29fc65dc3321958/tumblr_o9zo20vMVP1uom2zvo1_1280.png" ></figure> 
+0

、それは男が働いていません。 –

+0

は、を使用していない/>によって終了します。その場合、そのための正規表現は何ですか? @hardy rust –

+0

regexpを変更したときにあなたのコードが私のために働いていましたが、問題はがすべて文字列の最後にあることを意味した後に追加されますが、が終了した後に必要です。 –

0
$v['content_en'] = preg_replace('/(<img[^>]+>(?:<\/img>)?)/i', '<figure>$1<figure />', $v['content_en']); 
関連する問題