求从一个html代码中提取出图片的地址的方法 $re = "#(<img.+?src=(['\"]?))(.+)(\/.+?)\\2#";echo preg_replace( $re, "\\1images\\4\\2", $str); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那我怎么把http://url/x.jpg 这种地址取出来呢? to:ice_berg16(寻梦的稻草人) 如果我的$str="<img src=http://url/x.jpg alt=说明 Width=450 border=1 height=337>文字<img src=http://url/sx.jpg>";那么替换后的结果不是<img src=images/x.jpg alt=说明 Width=450 border=1 height=337>文字<img src=images/sx.jpg>了 漏了用这个$re = "#(<img.+?src=(['\"]?))([^ >]+)(\/.+?)\\2#"; 我通过if(preg_match($re,$str,$results)){echo $results[3].$result[4];} 这个取出来的值是http://url/x剩下的.jpg呢? 还有我的$str="<img src=http://url/x.jpg alt=说明 Width=450 border=1 height=337>文字<img src=http://url/sx.jpg>";如果是这个的话if(preg_match($re,$str,$results)){echo $results[3].$result[4];} 就没用了 $re = "#(<img.+?src=(['\"]?))([^ >]+)(\/.+?)\\2#";echo preg_replace( $re, "\\1images\\4\\2", $str);我给出的就是替换后的结果如果你用preg_match只能取一次,匹配全部得使用preg_match_allprint_r($results);根据你的需要可以组合$results的元素来得到你想要的部分. 我组合过了,但是始终.jpg没出来,不知在哪 是没有.jpg我并没有捕获扩展名,因为你的要求中只是替换前边的部分,所以不需要捕获扩展名下边这个可以取得扩展名$re = "#(<img.+?src=(['\"]?))([^ >]+)(\/.+?(\.\w+))\\2#"; 最后程序如下,但还有一点问题,请帮忙看看<?php$str="<img src=http://url/xx.jpg alt=说明 Width=450 border=1 height=337>中华人民共和国<img src=http://1url/sx.jpg><img src=2url/sx.jpg alt=0>";//原内容$re = "#(<img.+?src=(['\"]?))([^ >]+)(.+?)\\2#";//正则模式if(preg_match_all($re,$str,$results,PREG_SET_ORDER)){//如果有匹配for($i=0;$i<count($results);$i++){//循环取得图片地址$getimgurl[$i]=$results[$i][3];//依次取得得图片地址并赋值给数组getimgurl$filename="img".$i.".jpg";//产生新文件名$changecontent=preg_replace($re, "\\1$filename\\2\\4", $str);//使原字串中图片地址变成新产生的文件名}应如何更正?} echo $changecontent;?>最后输出的值$changecontent;是<img src=img2.jpg alt=说明 Width=450 border=1 height=337>中华人民共和国<img src=img2.jpg><img src=img2.jpg alt=0>这样三个图片文件名一样,显然不是我想要的而我想是<img src=img0.jpg alt=说明 Width=450 border=1 height=337>中华人民共和国<img src=img1.jpg><img src=img2.jpg alt=0>像这样的结果 $str="<img src=http://url/xx.jpg alt=说明 Width=450 border=1 height=337>中华人民共和国<img src=http://1url/sx.jpg><img src=2url/sx.jpg alt=0>";//原内容$re = "#(<img.+?src=(['\"]?)).+?(\.[^ >]+)\\2#";//正则模式if(preg_match_all($re,$str,$results,PREG_SET_ORDER)){//如果有匹配for($i=0;$i<count($results);$i++){//循环取得图片地址$getimgurl[$i]=$results[$i][3];//依次取得得图片地址并赋值给数组getimgurl$filename="img".$i.".jpg";//产生新文件名$replace = $results[$i][1] . $filename . $results[$i][2];$str=str_replace($results[$i][0], $replace, $str);//使原字串中图片地址变成新产生的文件名}} echo $str; (散分)这段代码把我整疯了——闲着无聊就来看看这问题吧 rewrite URL转换 求好一点的php在线编辑器 最新的mysql支持存储过程吗? 问一个菜问题:为什么有PHP画图时,纯PHP代码时可以显示,加入网页元素后就出现乱码呢? 谁有php的md5 加密文件啊?? 问一段代码 如何把unicode转换为Gb2312 _-_-_-_-_-phphelp寻求发展意见_-_-_-_-_- php为什么要有push pop等 想用来打印的层,能不能不显示? MySql Errno 1250 是什么原因啊???
如果我的
$str="<img src=http://url/x.jpg alt=说明 Width=450 border=1 height=337>文字<img src=http://url/sx.jpg>";
那么替换后的结果不是
<img src=images/x.jpg alt=说明 Width=450 border=1 height=337>文字<img src=images/sx.jpg>了
用这个
$re = "#(<img.+?src=(['\"]?))([^ >]+)(\/.+?)\\2#";
if(preg_match($re,$str,$results)){
echo $results[3].$result[4];
}
这个取出来的值是http://url/x
剩下的.jpg呢?
如果是这个的话
if(preg_match($re,$str,$results)){
echo $results[3].$result[4];
}
就没用了
echo preg_replace( $re, "\\1images\\4\\2", $str);我给出的就是替换后的结果如果你用preg_match只能取一次,匹配全部得使用preg_match_allprint_r($results);
根据你的需要可以组合$results的元素来得到你想要的部分.
我并没有捕获扩展名,因为你的要求中只是替换前边的部分,所以不需要捕获扩展名下边这个可以取得扩展名
$re = "#(<img.+?src=(['\"]?))([^ >]+)(\/.+?(\.\w+))\\2#";
$str="<img src=http://url/xx.jpg alt=说明 Width=450 border=1 height=337>中华人民共和国<img src=http://1url/sx.jpg><img src=2url/sx.jpg alt=0>";//原内容$re = "#(<img.+?src=(['\"]?))([^ >]+)(.+?)\\2#";//正则模式if(preg_match_all($re,$str,$results,PREG_SET_ORDER)){//如果有匹配for($i=0;$i<count($results);$i++){//循环取得图片地址$getimgurl[$i]=$results[$i][3];//依次取得得图片地址并赋值给数组getimgurl$filename="img".$i.".jpg";//产生新文件名$changecontent=preg_replace($re, "\\1$filename\\2\\4", $str);//使原字串中图片地址变成新产生的文件名
}
应如何更正?}
echo $changecontent;
?>最后输出的值$changecontent;是
<img src=img2.jpg alt=说明 Width=450 border=1 height=337>中华人民共和国<img src=img2.jpg><img src=img2.jpg alt=0>
这样三个图片文件名一样,显然不是我想要的而我想是<img src=img0.jpg alt=说明 Width=450 border=1 height=337>中华人民共和国<img src=img1.jpg><img src=img2.jpg alt=0>像这样的结果
for($i=0;$i<count($results);$i++){//循环取得图片地址$getimgurl[$i]=$results[$i][3];//依次取得得图片地址并赋值给数组getimgurl$filename="img".$i.".jpg";//产生新文件名
$replace = $results[$i][1] . $filename . $results[$i][2];
$str=str_replace($results[$i][0], $replace, $str);//使原字串中图片地址变成新产生的文件名
}}
echo $str;