【发布时间】:2013-04-05 02:47:31
【问题描述】:
我正在构建一个解析西班牙语词典的网站。如果您查找单词 HOLA,您将收到定义,但换句话说,您会收到建议,例如 CASA:http://verbum.xtrweb.com/verbumpost.php?word0=hola&word-1=casa
我希望:当您单击建议时(例如我上面发布的示例中的 CASAR)以将结果打印到像 HOLA 这样的 div 中。这是我目前使用的代码:
$words = array('word0','word-1');
function url_decode($string){
return urldecode(utf8_decode($string));
}
$baseUrl = 'http://lema.rae.es/drae/srv/search?val=';
$cssReplace = <<<EOT
<style type="text/css">
// I changed the style
</style>
</head>
EOT;
$resultIndex = 0;
foreach($words as $word) {
if(!isset($_REQUEST[$word]))
continue;
$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word])));
$contents = str_replace('</head>', $cssReplace, $contents);
$contents = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/search', $contents);
echo "<div style='
//style
", (++$resultIndex) ,"'>", $contents,
"</div>";
}
我开始写代码了,请耐心等待,我也尝试了一些朋友推荐的DOM代码,但没有成功。
【问题讨论】:
-
我认为你应该为你想要完成的事情寻找 API,而不是解析 HTML。
-
最简单的方法是将结果加载到服务器调用的 div 中,但将其隐藏。然后根据点击更改可见性。您可以使用 AJAX,但它会有点矫枉过正,这里绝对不需要。
标签: php jquery html php-parser