我在让详细的 preg_match_all 工作时遇到问题。我不断得到一个空白数组。
这是我的代码:
<?php
$remote_search = file_get_contents('http://wiki.seg.org/index.php?title=Special%3ASearch&search=drilling&button=');
preg_match_all('%<li><div class=\'mw-search-result-heading\'><a href="(.*)" title="(.*)">(.*)</a> </div> <div class=\'searchresult\'>(.*)</div>
<div class=\'mw-search-result-data\'>(.*)</div></li>%si', $remote_search, $links);
echo '<ul class=\'mw-search-results\'>';
for($i = 0; $i < count($links[1]); $i++) {
echo '<li><div class=\'mw-search-result-heading\'><a href="' . $links[5][$i] . '" title="' . $links[4][$i] . '">' . $links[3][$i] . '<\/a> </div> <div class=\'searchresult\'>' . $links[2][$i] . '<\/div><div class=\'mw-search-result-data\'>' . $links[1][$i] . '<\/div><\/li>';
}
echo '</ul>';
?>
我正在尝试从下面显示的代码中获取链接详细信息:
<li><div class='mw-search-result-heading'><a href="/index.php/Dictionary:Cable_drilling" title="Dictionary:Cable drilling">Dictionary:Cable drilling</a> </div> <div class='searchresult'>{{lowercase}}{{#category_index:C|cable <span class='searchmatch'>drilling</span>}}
</div>
<div class='mw-search-result-data'>132 B (22 words) - 19:58, 20 December 2011</div></li>
当我执行时,var_dump($links);
我得到Array
了结果。
下面的代码用于获取我试图提取变量的部分中的内容。
<?php
$remote_search = file_get_contents('http://wiki.seg.org/index.php?title=Special%3ASearch&search=drilling&button=');
preg_match_all('%<ul class=\'mw-search-results\'>(.*)</ul>%si', $remote_search, $links);
$bar = $links[0];
echo '<ul class=\'mw-search-results\'>';
echo $bar;
echo '</ul>';
var_dump($links);
?>
结果echo $bar;
输入Array
,没有输出。
此var_dump($links);
代码段中的 输出 ul 的内容。
有没有人在我的顶部代码段中看到阻止我按预期方式解析代码的错误?