PHP Simple HTML DOM Parser find string

前端 未结 4 767
心在旅途
心在旅途 2020-12-31 18:52

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

4条回答
  •  独厮守ぢ
    2020-12-31 19:18

    $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';
    

提交回复
热议问题