简体   繁体   English

从PHP中的Wordreference API中删除元素

[英]Removing element from wordreference API in PHP

How can I remove the elements I desire (for example, the search box) from by instructions in PHP? 如何通过PHP中的说明删除所需的元素(例如,搜索框)?

Link : http://api.wordreference.com/38159/enes/hello

NOTE: in my site I first request what you see in the link above and paste it in a div, if this info is of any use 注意:在我的网站上,如果此信息有用,我首先要求您在上面的链接中看到什么并将其粘贴到div中

For searching a HTML-tree for certain elements, which is what you seem to DOMXPath for searching the tree of an XML-tree. 用于在HTML树中搜索某些元素,这在DOMXPath中看起来就像在搜索XML树的树。

Have a look at: http://www.php.net/manual/en/domxpath.query.php Remember however that searching an XML tree is not the cheapest of operations, so use it with caution. 看一下: http: //www.php.net/manual/en/domxpath.query.php但是请记住,搜索XML树并不是最便宜的操作,因此请谨慎使用。

Writing XPath queries can be tricky - but Firebug can be used to easily form them ( in firebug, go to some element of desire and hover the "CSS path" firebug is showing - the tooltip contains the XPath for the same...) 编写XPath查询可能很棘手-但是可以使用Firebug轻松地形成它们(在Firebug中,转到所需的某些元素,然后将鼠标悬停在Firebug显示的“ CSS路径”上-工具提示包含相同的XPath ...)

Well, asuming that you for example want to get the content, try: $word = "hello"; 好吧,假设您想获取内容,请尝试:$ word =“ hello”;

$doc = new DOMDocument;

// We don't want to bother with white spaces.
$doc->preserveWhiteSpace = false;

// preg_replace is used for security, all except a-z and 0-9, and underscore will be replaced with nothing.
$url = "http://api.wordreference.com/38159/enes/" . preg_replace( "/[^\w ]/", "", $word );

$doc -> Load( $url );

$xpath = new DOMXPath($doc);

// We start from the root element.
$entries = $xpath -> query( "//html/body/div/div[3]" );

// do some stuff with the elements found...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM