求替换全局img图片的正则表达式 正则phpimg上传 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $content = '<p><img src="http://localhost:8080/story/images/tmp/1403530150545.jpg" style="width: 268px;"><img src="http://localhost:8080/story/images/tmp/1403530147265.jpg" style="width: 268px;">test</p>';$content = preg_replace('#(?<=src="http://localhost:8080/story/images/)tmp/#', 'pub/', $content);echo $content;<p><img src="http://localhost:8080/story/images/pub/1403530150545.jpg" style="width: 268px;"><img src="http://localhost:8080/story/images/pub/1403530147265.jpg" style="width: 268px;">test</p> 你没有防止贪婪匹配。 $pattern='/(<[img|IMG].+?src=\"?.+?)(images\/tmp\/)(.+?\.(jpg|gif|bmp|bnp|png)\"?.+?>)/'; 你写的方法加一个参数U就可以了。加上U,将懒惰匹配 变成 贪婪匹配。$pattern='/(<[img|IMG].+src=\"?.+)(images\/tmp\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/U';测试例子:$content = '<p><img src="http://localhost:8080/story/images/tmp/1403530150545.jpg" style="width: 268px;"><img src="http://localhost:8080/story/images/tmp/1403530147265.jpg" style="width: 268px;">test</p>';replace_img_publish_path($content);function replace_img_publish_path($content){ $pattern='/(<[img|IMG].+src=\"?.+)(images\/tmp\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/U'; $replacement="\${1}images/pub/\${3}"; print preg_replace($pattern, $replacement, $content); exit;}替换后:<p><img src="http://localhost:8080/story/images/pub/1403530150545.jpg" style="width: 268px;"><img src="http://localhost:8080/story/images/pub/1403530147265.jpg" style="width: 268px;">test</p> 求助,php代码写一个生成桌面快捷方式 如何实现网站自动发手机短信功能,谢谢 已经安装了jpegsrc,但php还是不支持image的jpeg函数 在一个循环中,如何得到其中最大值, 浏览记录可以,添加就不行? 奇怪了header不能转向啊?? 我这样一句SQL语句,怎么得不出结果? 问一个读数据库的问题? php中用javascript的问题(符代码) 上传文件时只要图片,而且对大小和尺寸都要限制,怎么版? 学习php 类,遇到一个基础问题,求好心人解答。 某网盘里的压缩包rar格式外链有referer检测防盗链,求php
echo $content;<p><img src="http://localhost:8080/story/images/pub/1403530150545.jpg" style="width: 268px;"><img src="http://localhost:8080/story/images/pub/1403530147265.jpg" style="width: 268px;">test</p>
加上U,将懒惰匹配 变成 贪婪匹配。$pattern='/(<[img|IMG].+src=\"?.+)(images\/tmp\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/U';测试例子:$content = '<p><img src="http://localhost:8080/story/images/tmp/1403530150545.jpg" style="width: 268px;"><img src="http://localhost:8080/story/images/tmp/1403530147265.jpg" style="width: 268px;">test</p>';replace_img_publish_path($content);function replace_img_publish_path($content){
$pattern='/(<[img|IMG].+src=\"?.+)(images\/tmp\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/U';
$replacement="\${1}images/pub/\${3}";
print preg_replace($pattern, $replacement, $content);
exit;
}
替换后:
<p><img src="http://localhost:8080/story/images/pub/1403530150545.jpg" style="width: 268px;"><img src="http://localhost:8080/story/images/pub/1403530147265.jpg" style="width: 268px;">test</p>