Home > Backend Development > PHP Tutorial > php 正则匹配 url img

php 正则匹配 url img

WBOY
Release: 2016-06-23 14:13:45
Original
1054 people have browsed it

PHP URL 正则 图片 url

我现在要匹配一个url地址,如:php 正则匹配 url img。我只想要src的url地址,还要匹配php 正则匹配 url img,有的是这种形式php 正则匹配 url imgsrc后面没有““”双引号,有的是单引号,而且alt和title有的php 正则匹配 url img标记中是没有的。请问各位这个正则怎么写呢?

回复讨论(解决方案)

/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(jpg|gif|png)/

<?php$str =<<<EOF<img alt="图片说明" src="http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg" title="title" />src后面没有““”双引号<img alt="图片说明" src=http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg title="title" />单引号<img alt="图片说明" src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' title="title" />没有alt<img  src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' title="title" / alt="php 正则匹配 url img" >没有title<img alt="图片说明" src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' title="title" />都没有<img  src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' / alt="php 正则匹配 url img" >EOF;$reg = '/<img [^ alt="php 正则匹配 url img" >]*src=[\'"]?([^\'"\s]+)[\'"]?[^>]*>/im';$matches = array();preg_match_all($reg, $str, $matches);print_r($matches);
Copy after login
Copy after login
Copy after login

<?php$str =<<<EOF<img alt="图片说明" src="http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg" title="title" />src后面没有““”双引号<img alt="图片说明" src=http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg title="title" />单引号<img alt="图片说明" src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' title="title" />没有alt<img  src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' title="title" / alt="php 正则匹配 url img" >没有title<img alt="图片说明" src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' title="title" />都没有<img  src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' / alt="php 正则匹配 url img" >EOF;$reg = '/<img [^ alt="php 正则匹配 url img" >]*src=[\'"]?([^\'"\s]+)[\'"]?[^>]*>/im';$matches = array();preg_match_all($reg, $str, $matches);print_r($matches);
Copy after login
Copy after login
Copy after login



好像不能匹配
>


<?php$str =<<<EOF<img alt="图片说明" src="http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg" title="title" />src后面没有““”双引号<img alt="图片说明" src=http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg title="title" />单引号<img alt="图片说明" src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' title="title" />没有alt<img  src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' title="title" / alt="php 正则匹配 url img" >没有title<img alt="图片说明" src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' title="title" />都没有<img  src='http://i3.sinaimg.cn/ent/2013/0604/U8551P28DT20130604154106.jpg' / alt="php 正则匹配 url img" >EOF;$reg = '/<img [^ alt="php 正则匹配 url img" >]*src=[\'"]?([^\'"\s]+)[\'"]?[^>]*>/im';$matches = array();preg_match_all($reg, $str, $matches);print_r($matches);
Copy after login
Copy after login
Copy after login



好像不能匹配
php 正则匹配 url img" src='a.jpg' /> 


举例子的话最好举一个符合html规范的例子,你说的那个例子无意义。

多谢各位!!!

Related labels:
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