日期:2014-05-17  浏览次数:20444 次

跪求正则表达式!!!
求正则表达式提取第一个url(直接取出最后那个id最佳),超链接的内容,提问日期那个时间,后面那个超链接内容,以及回答数那个数字,这个要preg_match_all循环匹配的,各位大牛帮帮小弟啊!
<li>
<dl>
<dt>
<a href="http://url/question_detail/q1187216696">我擦我擦我擦擦擦<wbr>。
我擦我擦我擦擦擦<wbr></a>

</dt>
<dd>提问日期:2012/05/12 18:06:58 - <a href="http://url2">kagakugizyutugakari</a>我擦 - 回答数:0 - 悬赏:<img alt="我又擦" src="http://url/i_coin.gif" align="absmiddle">50<br>
<a href="http://urls">我擦 &gt; 我擦 &gt; 我擦</a></dd>
</dl>
</li>

------解决方案--------------------
结果出来了,就是回朔多了点~凑合用吧
PHP code
<?php 
$str=<<<HTML
<li>
<dl>
<dt>
<a href="http://url/question_detail/q1187216696">我擦我擦我擦擦擦<wbr>。
我擦我擦我擦擦擦<wbr></a>

</dt>
<dd>提问日期:2012/05/12 18:06:58 - <a href="http://url2">kagakugizyutugakari</a>我擦 - 回答数:0 - 悬赏:<img alt="我又擦" src="http://url/i_coin.gif" align="absmiddle">50<br>
<a href="http://urls">我擦 &gt; 我擦 &gt; 我擦</a></dd>
</dl>
</li>
HTML;

$pattern='/
question_detail\/([^"]+)">    #第一个id
(.+?)<\/a>                #第一个链接内容
.+?提问日期:([^-]+).+?        #提问时间
<a.+?([^<>]+)<\/a>        #第二个链接内容
.+?回答数:(\d+)            #回答数
/xis';

preg_match_all($pattern,$str,$matches);
print_r($matches);

/* 
Array
(
    [0] => Array
        (
            [0] => question_detail/q1187216696">我擦我擦我擦擦擦。
我擦我擦我擦擦擦


提问日期:2012/05/12 18:06:58 - kagakugizyutugakari我擦 - 回答数:0
        )

    [1] => Array
        (
            [0] => q1187216696
        )

    [2] => Array
        (
            [0] => 我擦我擦我擦擦擦。
我擦我擦我擦擦擦
        )

    [3] => Array
        (
            [0] => 2012/05/12 18:06:58 
        )

    [4] => Array
        (
            [0] => kagakugizyutugakari
        )

    [5] => Array
        (
            [0] => 0
        )

)*/

------解决方案--------------------
PHP code
[User:root Time:18:44:25 Path:/home/liangdong/php]$ php preg.php 
Array
(
    [0] => <a href="http://url/question_detail/q1187216696">我擦我擦我擦擦擦<wbr>。我擦我擦我擦擦擦<wbr></a>
    [1] => q1187216696
)
Array
(
    [0] => <dd>提问日期:2012/05/12 18:06:58 - <a href="http://url2">kagakugizyutugakari</a>
    [1] => 2012/05/12 18:06:58
    [2] => kagakugizyutugakari
)
[User:root Time:18:44:26 Path:/home/liangdong/php]$ cat preg.php 
<?php
$str = <<<EOF
<li>
<dl>
<dt>
<a href="http://url/question_detail/q1187216696">我擦我擦我擦擦擦<wbr>。我擦我擦我擦擦擦<wbr></a>

</dt>
<dd>提问日期:2012/05/12 18:06:58 - <a href="http://url2">kagakugizyutugakari</a>我擦 - 回答数:0 - 悬赏:<img alt="我又擦" src="http://url/i_coin.gif" align="absmiddle">50<br>
<a href="http://urls">我擦 &gt; 我擦 &gt; 我擦</a></dd>
</dl>
</li>
EOF;

$nmatches = preg_match('/<a href="(?:(?-U)[^"]*)\/([^"]*)">.*<\/a>/Uis', $str, $matches);
print_r($matches);

$nmatches = preg_match('/<dd>.*(\d{4}\S+\s+\S+)\s*-\s*.*<a href=".*">(.*)<\/a>/Uis', $str, $matches);
print_r($matches);
?>