宽度不定的图片
$str = '<p><br></p><img style="width: 513px;" src="http://xxxx/uploads/details/142163431741415.jpg"><img style="width: 513px;" src="http://xxxx/uploads/details/14216343172321.jpg"><p><br></p>';
$str = '<p><br></p><img style="width: 100%;" src="http://xxxx/uploads/details/142163431741415.jpg"><img style="width: 100%;" src="http://xxxx/uploads/details/14216343172321.jpg"><p><br></p>';
$str = '<p><br></p><img style="width: 513px;" src="http://xxxx/uploads/details/142163431741415.jpg"><img style="width: 613px;" src="http://xxxx/uploads/details/14216343172321.jpg"><p><br></p>';echo preg_replace('/(?<=style="width:).+?px;/i', '100%', $str);e388a4556c0f65e1904146cc1a846bee0c6dc11e160d3b678d68754cc175188a94b3e26ee717c64999d7867364b1b4a3e73bedbd28c8e7575351313fd2a684d454f0a89eae6099af893502db6bc6cfdae388a4556c0f65e1904146cc1a846bee0c6dc11e160d3b678d68754cc175188a94b3e26ee717c64999d7867364b1b4a3
echo preg_replace('/(?<=<img )style="width\: .+?"/s', 'style="width: 100%;"', $str);
楼上 好像只能替换第一张图片的宽度,后面的无效
@xuzuning 怎么加上img限制
你测试了没有啊? 代码贴出来看看。
楼上 好像只能替换第一张图片的宽度,后面的无效
$str = '<p><br></p><img style="width: 513px;" src="http://admin.zhiai/uploads/details/142163431741415.jpg"><p><br></p><img src="http://admin.zhiai/uploads/details/142163713729385.jpg" style="width: 513px;"><p><br></p><img src="http://admin.zhiai/uploads/details/142163714584100.jpg" style="width: 513px;"><p><br></p><img src="http://admin.zhiai/uploads/details/142163715250341.jpg" style="width: 513px;"><p><br></p>';//echo preg_replace('/(?<=style="width:).+?px;/i', '100%', $str);echo preg_replace('/(?<=<img )style="width\: .+?"/s', 'style="width: 100%;"', $str);
这样呢
echo preg_replace('/(?<=<img)(.+?)style="width\: .+?"/s', '$1style="width: 100%;"', $str);