mysql 替换html标签,替换HTML标签中的图片src?

I want to use regex to replace src html attributes. The HTML is not malformed and fortunately takes the same form in all the pages in the database - i.e.

1.png

I have code that works fine if there's only one image in the page. I want to know the best way to replace multiple images, as this one will replace all the image tags with the same string.

$result = $s->db_query("SELECT reviewFullText as f FROM reviews WHERE reviewsID = 155");

while($row = mysql_fetch_array($result))

{

$body = stripslashes(html_entity_decode($row['f'], ENT_NOQUOTES, "UTF-8"));

preg_match_all('/]{1}/i', $body, $matches);

for($i=0;$i

{

$number = preg_replace("/[^0-9]/", '', $matches[0][$i]);

echo preg_replace('/]{1}/i', ''%20.%20%24number%20.%20'.png

}

}

So if the page contains two files, one called 1.png and one called 2.png the script should parse the numbers and replace them with a different url such as http://x.y/a/1.png and http://x.y/a/2.png.

I've heard preg_replace_callback is the best way to do this but I have no idea how to get this working... Help!

解决方案# Untested code:

$xml = new SimpleXml($xmlString);

foreach ($xml->xpath('//img') as $imgNode) {

$imgNode->addAttribute('src', "http://x.y/a/" . $imgNode->getAttribute('src'));

}

echo $xml->asXML();

Note that you will need something like DOMDocument::loadHtml(), if your html is not xhtml (i.e. valid xml), but the idea remains the same.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值