使用preg_match刮取html页面

时间:2013-09-07 08:51:27

标签: php preg-match

<?php 
    $contents = file_get_contents('link here');
    $doc = new DOMDocument();
    @$doc->loadHTML($contents);
    $xpath = new DOMXPath($doc);
    $xquery = '//tr[td[a]]';           
    $links = $xpath->query($xquery);   
    foreach ($links as $el) {
        $string = ($doc->saveHTML($el));
        preg_match('/<a class="LN" href=".*" onclick=".*">(.*)<\/a>/i', $string, $name); 
        preg_match('/<td align="center">.*\s*<\/td>\s*<td>(.*)<\/td>/i', $string, $locations);
        echo strip_tags($name[1]).' '.strip_tags($locations[1]);
    } 
?>

我的$ string的值是

  <tr>
<td>
1.
 </td>
<td>
<a class="LN" href="" onclick="">
<b>Aaberg, Aaron E</b></a>
</td>
<td align="center">
54 
</td>
<td><a href="">Anchorage, AK</a><br />
<a href="">Nondalton, AK</a><br />
</td>
<td> </td><td><a href="">
                    View Details
                  </a></td></tr>

为什么我无法获得我的$ location [i]?

2 个答案:

答案 0 :(得分:1)

这个怎么样?

<?php

$contents = file_get_contents('http://....');

$pattern =
'@' . 
'<td>\s*+' .
'(?P<no>\d+)\.\s*+' .
'</td>\s*+' .
'<td>\s*+' .
'<a class="LN" href="[^"]*+" onclick="[^"]*+">\s*+' .
'<b>(?P<name>[^<]*+)</b>\s*+' .
'</a>\s*+' .
'</td>\s*+' .
'<td align="center">[^<]*+</td>\s*+' .
'<td>\s*+' .
'(?P<locations>(?:<a href="[^"]*+">[^<]*+</a><br />\s*+)++)' .
'</td>' .
'@'
;

$results = array();
preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER);
foreach ($matches as $i => $match) {
    preg_match_all('@<a href="[^"]*+">([^<]*+)</a>@', $match['locations'], $locations);
    $results[$i]['no'] = $match['no'];
    $results[$i]['name'] = $match['name'];
    $results[$i]['locations'] = $locations[1];
}

echo '<pre>';
print_r($results);
echo '</pre>';

这将完美无缺。

答案 1 :(得分:0)

而不是

preg_match('/<td align="center">.*\s*<\/td>\s*<td>(.*)<\/td>/i', $string, $locations);

你可以使用

preg_match('/<a href="">(.+)<\/a><br \/>/i', $string, $locations)