【问题标题】:preg_match_all , get all img tag that include a stringpreg_match_all , 获取所有包含字符串的 img 标签
【发布时间】:2011-05-30 12:52:47
【问题描述】:

这段代码获取所有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">

【问题讨论】:

  • 哦,我喜欢这个。我要试试看。

标签: php regex preg-match-all


【解决方案1】:

我必须选择这个:

/(<img[^>]*src=".*?(?:pre\.gif|next\.gif)"[^>]*>)/i

或者在 PHP 中:

$regexp = '/(<img[^>]*src=".*?(?:pre\.gif|next\.gif)"[^>]*>)/i';
$iResults = preg_match_all($regexp, $str, $aMatches);
print_r($aMatches); // you'll see what you need

-- 编辑: 哎呀。我犯了一个错误。正则表达式中的pre.gifnext.gif 中的. 必须对正则表达式进行转义!!我以前没有。 -- 编辑

PS。您可能使用 preg_match_all 错误。参数是:(patternsubject&amp;matches

PS。我的模式+你的主题的结果:

大批 ( [0] => 数组 ( [0] => [1] => ) [1] => 数组 ( [0] => [1] => ) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    相关资源
    最近更新 更多