PHP简单HTML DOM解析器查找字符串 [英] PHP Simple HTML DOM Parser find string

查看:189
本文介绍了PHP简单HTML DOM解析器查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP简单的DOM解析器,但它似乎没有搜索文本的功能。我需要搜索一个字符串并找到它的父ID。任何人都知道如何?

解决方案

  $ d = new DOMDocument(); 
$ d-> loadXML($ xml);
$ x = new DOMXPath($ d);
$ result = $ x-> evaluate(// text()[contains(。,'617.99')] / ancestor :: * / @ id);
$ unique = null; ($ i = $ result-> length -1; $ i> = 0&&$&$ item = $ result-> item($ i); $ i - ){
if($ x-> query(// * [@ id ='。addslashes($ item-> value)。']) - > length == 1){
echo'Unique ID is'。$ item-> value。\\\
;
$ unique = $ item-> value;
break;
}
}
if(is_null($ unique))echo'没有找到唯一的ID';


I am using PHP simple DOM parser but it does not seem to have the functionality to search for text. I need to search for a string and find the parent id for it. Essentially the reverse of normal usage.

Anyone know how?

解决方案

$d = new DOMDocument();
$d->loadXML($xml);
$x = new DOMXPath($d);
$result = $x->evaluate("//text()[contains(.,'617.99')]/ancestor::*/@id");
$unique = null;
for($i = $result->length -1;$i >= 0 && $item = $result->item($i);$i--){
    if($x->query("//*[@id='".addslashes($item->value)."']")->length == 1){
        echo 'Unique ID is '.$item->value."\n";
            $unique = $item->value;
        break;
    }
}
if(is_null($unique)) echo 'no unique ID found';

这篇关于PHP简单HTML DOM解析器查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆