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

求教正则写法
$str = '<ul>
<li>
<p><a href="http://aaa.com/moming" target="_blank"><img src="/images/j22.jpg"></a></p>
<p class="b">莫名</p>
<p>粉丝:932</p>
<p>风险投资人</p>
</li>
<li>
<p><a href="http://aaa.com/yxian" target="_blank"><img src="/images/j7.jpg"></a></p>
<p class="b">逸仙<</p>
<p>粉丝:993</p>
<p>财经</p>
</li>
<li>
<p><a href="http://aaa.com/ikco" target="_blank"><img src="/images/j2.jpg"></a></p>
<p class="b">称王</p>
<p>粉丝:1093</p>
<p>IT</p>
</li>
</ul>
  ';

这段字符串怎么匹配出每个li的url,imgurl,name,fans,job,如第一<li>应该分别匹配出http://aaa.com/iclock, /images/j22.jpg,莫名,932,风险投资人

------解决方案--------------------
PHP code
[User:root Time:06:22:25 Path:/home/liangdong/php]$ php preg.php 
Array
(
    [0] => Array
        (
            [0] => <li>
<p><a href="http://aaa.com/moming" target="_blank"><img src="/images/j22.jpg"></a></p>
<p class="b">莫名</p>
<p>粉丝:932</p>
<p>风险投资人</p>
</li>
            [1] => http://aaa.com/moming
            [2] => /images/j22.jpg
            [3] => 莫名
            [4] => 粉丝:932
            [5] => 风险投资人
        )

    [1] => Array
        (
            [0] => <li>
<p><a href="http://aaa.com/yxian" target="_blank"><img src="/images/j7.jpg"></a></p>
<p class="b">逸仙<</p>
<p>粉丝:993</p>
<p>财经</p>
</li>
            [1] => http://aaa.com/yxian
            [2] => /images/j7.jpg
            [3] => 逸仙<
            [4] => 粉丝:993
            [5] => 财经
        )

    [2] => Array
        (
            [0] => <li>
<p><a href="http://aaa.com/ikco" target="_blank"><img src="/images/j2.jpg"></a></p>
<p class="b">称王</p>
<p>粉丝:1093</p>
<p>IT</p>
</li>
            [1] => http://aaa.com/ikco
            [2] => /images/j2.jpg
            [3] => 称王
            [4] => 粉丝:1093
            [5] => IT
        )

)
[User:root Time:06:22:26 Path:/home/liangdong/php]$ cat preg.php 
<?php
$str = <<<EOF
<ul>
<li>
<p><a href="http://aaa.com/moming" target="_blank"><img src="/images/j22.jpg"></a></p>
<p class="b">莫名</p>
<p>粉丝:932</p>
<p>风险投资人</p>
</li>
<li>
<p><a href="http://aaa.com/yxian" target="_blank"><img src="/images/j7.jpg"></a></p>
<p class="b">逸仙<</p>
<p>粉丝:993</p>
<p>财经</p>
</li>
<li>
<p><a href="http://aaa.com/ikco" target="_blank"><img src="/images/j2.jpg"></a></p>
<p class="b">称王</p>
<p>粉丝:1093</p>
<p>IT</p>
</li>
</ul>
EOF;
$n = preg_match_all('/<li>.*<p>.*<a.+href="(.*)".*>.*<img.+src="(.*)">.*<\/a>.*<\/p>.*<p.*>(.*)<\/p>.*<p.*>(.*)<\/p>.*<p>(.*)<\/p>.*<\/li>/iUs', $str, $matches, PREG_SET_ORDER);
if ($n) {
        print_r($matches);
}