php - 在 PHP 中的 <img> 标签的开头和结尾附加 <figure> 标签

标签 php html mysql string

我想在 image 的开头和结尾附加图形标签标签。

这是我到目前为止尝试过的方法。

<?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> 之后结束标签结束。

最佳答案

试试这个,如果我理解正确的话:

$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>

关于php - 在 PHP 中的 <img> 标签的开头和结尾附加 <figure> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40215895/

相关文章:

PHP/MySQL 按列对结果进行分组

php - 如何在 Windows 启动时运行 XAMPP?

html - 如何制作响应式视频容器div?

mysql - MariaDB Regex 仅替换字符的第一个实例

mySQL 语句查找带有尾部斜杠的几乎重复的数据

php - 当没有列关系时从第二个表中检索数据

PHP 正则表达式验证

javascript - 自动刷新 div 不起作用

php - 如何将 php 发送邮件表单与图像点击列表结合起来?

php - 使用 JOIN NULL 值匹配数据库中的列名