您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

求轮换全局img图片的正则表达式

2024/4/1 2:39:01发布20次查看
求替换全局img图片的正则表达式
如题
现在有一字符串是
$content = '
test
';
想要将这字符里面的 images/tmp 都替换成 images/pub
本人已经写了一个函数
public static function replace_img_publish_path($content){
$pattern='/()/';
$replacement=\${1}images/pub/\${3};
print preg_replace($pattern, $replacement, $content);
exit;
}

输出结果为
test
只替换了最后一个img标签
如何才能全部都替换?
------解决方案--------------------
$content = 'test
';
$content = preg_replace('#(?pub/1403530147265.jpg style=width: 268px;>test
------解决方案--------------------
你没有防止贪婪匹配。
$pattern='/()/';
------解决方案--------------------
你写的方法加一个参数u就可以了。
加上u,将懒惰匹配 变成 贪婪匹配。
$pattern='/()/u';
测试例子:
$content = 'test
';
replace_img_publish_path($content);
function replace_img_publish_path($content){
$pattern='/()/u';
$replacement=\${1}images/pub/\${3};
print preg_replace($pattern, $replacement, $content);
exit;
}

替换后:
test
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product