在PHP中的<img/>标记的开头和结尾附加<figure>标记

时间:2016-10-24 10:16:44

标签: 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>标记结束后结束。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

from bs4 import BeautifulSoup
import requests
import re

url = 'http://shakespeare.mit.edu/cymbeline/index.html'
html = requests.get(url, headers={
                        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64)AppleWebKit/537.36 (KHTML, like Gecko)Chrome/51.0.2704.84 Safari/537.36"}).content

bsObj = BeautifulSoup(html, "lxml")

links = bsObj.findAll('a', href=re.compile('(cymbeline)'))
finalLinks = []
for link in links:
    finalLinks.append('http://shakespeare.mit.edu/cymbeline/' + link.attrs['href'])

print finalLinks