preg_match_all ,获取所有包含字符串的 img 标签
这段代码获取所有 img 标签
preg_match_all('/<img[^>]+>/i',$a,$page);
,但我想要获取其文件名包含“next.gif”或“pre.gif”的标签
,例如:
$page = '
<img border="0" alt="icon" src="http://www.site.com/images/man.gif" width="90" height="90">
<img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v">
<img border="0" alt="icon" src="http://www.site.com/images/2.gif">
<img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
';
我的输出应该如下所示:
<img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="90">
<img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
this code get all img tags
preg_match_all('/<img[^>]+>/i',$a,$page);
but I want get tags that their filenames includes "next.gif" or "pre.gif"
for example :
$page = '
<img border="0" alt="icon" src="http://www.site.com/images/man.gif" width="90" height="90">
<img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v">
<img border="0" alt="icon" src="http://www.site.com/images/2.gif">
<img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
';
and I output should be like this :
<img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="90">
<img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须选择这个:
或者在 PHP 中:
- 编辑:
哎呀。我犯了一个错误。正则表达式中的
pre.gif
和next.gif
中的.
必须转义正则表达式!我以前没有。——编辑
PS。您可能使用了 preg_match_all 错误。参数为:(
pattern
、subject
、&matches
)PS。我的模式+你的主题的结果:
I'd have to go with this one:
Or in PHP:
-- edit:
Oops. I made a mistake. The
.
inpre.gif
andnext.gif
in the regexp the regexp must be escaped!! I didn't before.-- edit
PS. You might be using preg_match_all wrong. The arguments are: (
pattern
,subject
,&matches
)PS. The results of my pattern + your subject: