Home > Backend Development > PHP Tutorial > 正则问题 将图片宽度替换为100%

正则问题 将图片宽度替换为100%

WBOY
Release: 2016-06-23 13:41:11
Original
1363 people have browsed it

宽度不定的图片

$str = '<p><br></p><img    style="max-width:90%" src="http://xxxx/uploads/details/142163431741415.jpg" alt="正则问题 将图片宽度替换为100%" ><img    style="max-width:90%" src="http://xxxx/uploads/details/14216343172321.jpg" alt="正则问题 将图片宽度替换为100%" ><p><br></p>';
Copy after login
Copy after login


宽度替换为100%

$str = '<p><br></p><img    style="max-width:90%" src="http://xxxx/uploads/details/142163431741415.jpg" alt="正则问题 将图片宽度替换为100%" ><img    style="max-width:90%" src="http://xxxx/uploads/details/14216343172321.jpg" alt="正则问题 将图片宽度替换为100%" ><p><br></p>';
Copy after login
Copy after login


回复讨论(解决方案)

$str = '<p><br></p><img    style="max-width:90%" src="http://xxxx/uploads/details/142163431741415.jpg" alt="正则问题 将图片宽度替换为100%" ><img    style="max-width:90%" src="http://xxxx/uploads/details/14216343172321.jpg" alt="正则问题 将图片宽度替换为100%" ><p><br></p>';echo preg_replace('/(?<=style="width:).+?px;/i', '100%', $str);
Copy after login


http://xxxx/uploads/details/142163431741415.jpg"> http://xxxx/uploads/details/14216343172321.jpg">



echo preg_replace('/(?<=正则问题 将图片宽度替换为100% 

楼上 好像只能替换第一张图片的宽度,后面的无效

@xuzuning 怎么加上img限制

你测试了没有啊? 代码贴出来看看。

楼上 好像只能替换第一张图片的宽度,后面的无效

$str = '


正则问题 将图片宽度替换为100%


正则问题 将图片宽度替换为100%


正则问题 将图片宽度替换为100%


正则问题 将图片宽度替换为100%


';//echo preg_replace('/(?<=style="width:).+?px;/i', '100%', $str);echo preg_replace('/(?<=正则问题 将图片宽度替换为100%

这样呢

echo  preg_replace('/(?<=正则问题 将图片宽度替换为100% 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template