日期:2014-05-17 浏览次数:20610 次
<?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">我擦 > 我擦 > 我擦</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
)
)*/
------解决方案--------------------
[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">我擦 > 我擦 > 我擦</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);
?>