0
<?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]?

4

2 回答 2

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>';

这将完美地工作。

于 2013-09-07T09:45:08.960 回答
0

代替

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

您可以使用

preg_match('/<a href="">(.+)<\/a><br \/>/i', $string, $locations)
于 2013-09-07T08:56:50.633 回答