preg_match_all ,获取所有包含字符串的 img 标签

发布于 2024-10-08 19:32:31 字数 813 浏览 0 评论 0原文

这段代码获取所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与风相奔跑 2024-10-15 19:32:31

我必须选择这个:

/(<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&matches)

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

Array
(
    [0] => Array
        (
            [0] => <img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v">
            [1] => <img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
        )
    [1] => Array
        (
            [0] => <img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v">
            [1] => <img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
        )
)

I'd have to go with this one:

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

Or in 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

-- edit:
Oops. I made a mistake. The . in pre.gif and next.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:

Array
(
    [0] => Array
        (
            [0] => <img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v">
            [1] => <img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
        )
    [1] => Array
        (
            [0] => <img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v">
            [1] => <img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
        )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文