【问题标题】:preg_match_all() multiple items from textpreg_match_all() 文本中的多个项目
【发布时间】:2013-01-04 00:54:36
【问题描述】:

我有一个这样的数组:

<tr>
    <td class="vertTh">
        <center>
            <a href="/browse/200" title="More from this category">Video</a>
            <br />
            (
            <a href="/browse/201" title="More from this category">Movies</a>
            )
        </center>
    </td>
    <td>
        <div class="detName">
            <a href="/torrent/8036528/Life.of.Pi.2012.DVDSCR" class="detLink" title="Details for Life.of.Pi.2012.DVDSCR">Life.of.Pi.2012.DVDSCR</a>
        </div>
        <a href="magnet:?xt=urn:btih:b129c8fd1c91b00589ef8fe646f52ce10148a3c9&dn=Life.of.Pi.2012.DVDSCR&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80" title="Download this torrent using magnet">
            <img src="//static.thepiratebay.se/img/icon-magnet.gif" alt="Magnet link" />
        </a>
        <img src="//static.thepiratebay.se/img/icon_comment.gif" alt="This torrent has 68 comments." title="This torrent has 68 comments." />
        <img src="//static.thepiratebay.se/img/icon_image.gif" alt="This torrent has a cover image" title="This torrent has a cover image" />
        <a href="/user/scene4all">
            <img src="//static.thepiratebay.se/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border='0' />
        </a> <font class="detDesc">Uploaded 01-18&nbsp;17:41, Size 1.25&nbsp;GiB, ULed by
            <a class="detDesc" href="/user/scene4all/" title="Browse scene4all">scene4all</a></font> 
    </td>
    <td align="right">33981</td>
    <td align="right">18487</td>
</tr>

如何 preg_match()/preg_match_all()

我尝试使用这种模式:

<tr>
    <td class="vertTh">
        (?P<cat>.*?)
    </td>
    <td>
        <div class="detName">
            (?P<name>.*?)
        </div>
        (?P<link>.*?)
    </td>
    <td align="right">(?P<up>.*?)</td>
    <td align="right">(?P<down>.*?)</td>
</tr>

还有这段代码:

preg_match_all("#$pattern#s", $item, $v);
var_dump($v);

然后它返回:

array(11) {
  [0]=>
  array(0) {
  }
  ["cat"]=>
  array(0) {
  }
  [1]=>
  array(0) {
  }
  ["name"]=>
  array(0) {
  }
      ...
}

有人可以帮助我,如何修复此代码以返回实际内容? 我认为我提供的信息已经足够了。

【问题讨论】:

  • @Phil:该链接对任何人都没有帮助。这对我们来说很有趣,但对新手来说毫无用处。
  • 不要使用正则表达式解析 HTML。您无法使用正则表达式可靠地解析 HTML。一旦 HTML 与您的期望发生变化,您的代码就会被破坏。有关如何使用 PHP 模块正确解析 HTML 的示例,请参阅 htmlparsing.com/php.html

标签: php regex html-parsing preg-match preg-match-all


【解决方案1】:

我会分四步完成:

<?php
    preg_match_all('|category">([^<]*)</a>|isU', $html, $categories);
    preg_match('|<div class="detName">[^<]*<[^>]*>([^<]*)</a>|isU', $html, $name);
    preg_match('|<a href="(magnet:[^"]*)"|isU', $html, $link);
    preg_match_all('|<td align="right">([0-9]+)</td>|isU', $html, $up_down);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多