宽度不定的图片
$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);
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);