如果/ else条件,Xpath是否包含值PHP

时间:2019-07-12 07:32:02

标签: php dom xpath file-get-contents

尝试在任何网站内查找内容。如果内容存在,则需要做其他事情。

Xpath | PHP

   <?php 

     //Load the HTML page
     $html = file_get_contents('http://www.example.com/');
     //Parse it. Here we use loadHTML as a static method
     //to parse the HTML and create the DOM object in one go.
     @$dom = DOMDocument::loadHTML($html);

     //Init the XPath object
     $xpath = new DOMXpath($dom);

     $vals = $xpath->query( '//script[not[contains(text(), "sample")]]' );

        if (($vals) > 0) {

           echo 'finding text, not in the website';

        } else {

           echo "Test";

         }

    ?>

我无法探讨如何执行此条件。

1 个答案:

答案 0 :(得分:0)

您可以使用count()

if ((count($vals)) > 0) {
    echo 'finding text, not in the website';
} else {
    echo "Test";
}