我需要一种方法来获取具有特定ID的元素并使用PHP显示它。我是一个PHP noob,所以到目前为止这是非常困难的。所有其他类似的问题都有点复杂,所以我想知道是否有人可以向我解释。使用PHP获取网页的一部分
为了更具体地说明我想要什么,我正在为一个minecraft服务器进行拼写搜索。我们的网站是http://pvpzone.org/,wiki是http://pvpzone.wikispaces.com/。每个咒语在wiki上都有一个页面,就像pvpzone.wikispaces.com/Vanish中的“消失”一样。法术搜索的想法是寻找咒语的一种更简单的方法,您输入咒语名称并获得结果。 div'wiki wikiPage'中包含拼写数据。我想得到那个div并显示它。可悲的是我无法连接到任何形式的数据库,它是由Wikispaces托管的,他们不允许这样做。
我希望这已经很清楚,如果你愿意,可以问我更多的细节。以下是我迄今为止:
<?php
if(isset($_POST['submit']))
{
$spell=$_POST['spell'];
$pvpwiki="http://pvpzone.wikispaces.com/";
$site=$pvpwiki . $spell;
$submit=true;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Spell search</title>
</head>
<body>
<form name="spellsearch" id="spellsearchform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="spell" value="<?php if(!isset($_POST['submit'])){echo("Vanish");}?>"></input>
<input type="submit" value="Search" name="submit"></input>
</form>
<?php
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents($site));
var_dump($doc->getElementById('wiki wikiPage'));
if($doc == false && $submit)
{
echo("<br />" . "That is not a spell!");
}
?>
</body>
</html>
我的问题,现在是我得到一个语法错误(警告:DOM文档:: loadHTML()[domdocument.loadhtml]:在实体已经定义ID target_editor,行: 212 in /home/content/d/e/x/dext0459/html/russellsayshi/phpspellsearch.php on line 24 NULL),我真的很感激你的帮忙。
参见:http://stackoverflow.com/a/3614620/1329367 – Mahn
@马恩:好的提示,但事实证明,这不是问题。无论如何,如果*是*的问题,你可以得到具有重复'id'属性值与xpath的元素。 – hakre