很多站长朋友们都不太清楚php正则获取图片,今天小编就来给大家整理php正则获取图片,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php正则提取图片php正则 2、 如果用php正则获取图片路径前段部分? 3、 php中用正则表达式获取html中所有图片网址 4、 php正则匹配图片路径 5、 求php中正则表达式从html代码中获取图片路径 6、 php正则提取字符串中的 图片和超链接 保存为txt格式 php正则提取图片php正则写法很多,给你参考一种写法:
<?php
$url="<img src=\"\" alt=\"aaaa\" border=\"0\" />";
preg_match_all('/src\=\"[^\"]*\"/',$url,$match);
var_dump($match);
var_dump(substr($match[0][0],5,strlen($match[0][0])-6));
?>
如果用php正则获取图片路径前段部分?<?php
$img='esferereer<img src="/data/upload/help/202303/02/84d86516ec5118f2d8e86c1cf55293b1.gif" />ssrtwtwt';
$result = preg_replace ( "/.*<img[^>]*src[=\s\"\']+([^:]+:\/\/[\d\.]+\/[^\/]+\/).*/", "$1", $img );
echo $result . "\n";
?>
php中用正则表达式获取html中所有图片网址<?php
$html = '你的html代码';
preg_match_all('/\s+src\s?\=\s?[\'|"]([^\'|"]*)/is', $html, $Array);
print_r( $Array );
//$Array就是你想要的数组
php正则匹配图片路径给你个我写的,并在项目中使用了很长时间的正则吧.
/<img.*src\s*=\s*[\"|\']?\s*([^>\"\'\s]*)/i
,我使用kindeditor保存文章,但是需要取出第N个图片的地址作为文章的标志图片,文章代码(内容的html)保存到数据库一个字段,然后图片地址保存到另外一个字段.我就是使用上面的正则解决的.
我说明下,上面的地址是直接获取img标签内src属性的值.在使用该正则的php页面访问该路径如果能找到图片的话,可以直接使用,如果不能,你可以使用preg_match_all将所有地址先保存到数组,然后处理路径,比如获取文件名称(不含路径部分),然后重新组成url,再删除图片.
我的例子:
preg_match_all("/<img.*src\s*=\s*[\"|\']?\s*([^>\"\'\s]*)/i",str_ireplace("\\","",$content),$arr);
呵呵 我的内容部分被php给加上\转义了,所以我需要先把\去除,str_ireplace("\\","",$content),然后将匹配的内容保存到$arr数组(二维的).
$arr[1]就是存储该路径的数组.
求php中正则表达式从html代码中获取图片路径<?php
$test = '<p>444<img height="768" width="1024" alt="" src="/data/upload/help/202303/02/5d72bc7572bdf697a4415a56d081707e.jpg" /></p>
<p>444<img height="768" width="1024" alt="" src="/data/upload/help/202303/02/485533f2ad49f50fb886e5cf11e8aea4.jpg" /></p>
fsdafasdfasdfasdf
<p>444<img height="768" width="1024" alt="" src="/data/upload/help/202303/02/d537dbf12c888735f652c504b1303ebe.jpg" /></p>
sdfasdfasdf<p>
<p>444<img height="768" width="1024" alt="" src="/data/upload/help/202303/02/e26b2bf624ba9d20ee9179f6dbb8f827.jpg" /></p>
sdf32414撒旦发是否
<p>444<img height="768" width="1024" alt="" src="/data/upload/help/202303/02/406f51dd40a87ab875b61068111d6716.jpg" /></p>';
preg_match_all("/<p>.*src=\"([^^]*?)\".*<\/p>/i",$test,$match);
print_r($match[1]);
?>
这样应该可以,我试的多行的,中间还夹杂一些字符,没什么问题,呵呵
php正则提取字符串中的 图片和超链接 保存为txt格式php正则提取字符串中的图片和超链接并保存为txt格式文件的程序如下:
此程序提取图片和htm的超链接,并把它们存入1.txt文件.
<?php
$str = "<td> <table border=0 cellpadding=0 cellspacing=0> <tr> <td align=center> 省略一些~~~~~~~~~~~~~~
~~~ <tr> <td><a href=/news/2009-05-10/468487.htm title=撒旦发公司分公司大概使得法国 target=_blank><img
src=/files/images/18079.jpg width=105 height=121 border=0></a></td> </tr> 省略一些~~~~~~~~~~~~~~~~~
</td> </tr> <tr> <td align=center height=25><a href=/news/2009-05-10/468487.htm title=撒旦发公司分公司大概使得法国
target=_blank>撒旦发公司分公司大概使得法国</a></td> </tr> </table> </td> <td> 省略一些~~~~~~~~~~~~~~~~~
<td><a href=/news/2009-05-09/898449.htm title=阿道夫三个地方华盛顿国会 target=_blank><img src=/files/images/18068.jpg width=105
height=121 border=0></a></td> </tr> </table> </td> </tr> <tr> <td align=center
height=25><a href=/news/2009-05-09/898449.htm title=阿道夫三个地方华盛顿国会 target=_blank>阿道夫三个地方华盛顿国会</a></td> </tr>
</table> </td>";
preg_match_all("~<a href=(.*?)\s.+?(<img src=(.*?)\s.+?)?</a>~",$str,$matches);
$k=fopen("1.txt","a");
for($i=0;$i<count($matches[1]);$i++){
if($matches[3][$i]==""){
}else{
$s=$matches[1][$i]."".$matches[3][$i]."\r\n";
fwrite($k,$s);
}
}
fclose($k);
?>
运行结果:
1.txt文件内容
/news/2009-05-10/468487.htm/files/images/18079.jpg
/news/2009-05-09/898449.htm/files/images/18068.jpg
关于php正则获取图片的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php正则获取图片 php使用正则表达式的详细内容...