{"id":42,"date":"2025-04-07T18:56:32","date_gmt":"2025-04-07T10:56:32","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/?p=42"},"modified":"2025-04-07T18:56:32","modified_gmt":"2025-04-07T10:56:32","slug":"php-domxpath%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b%ef%bc%8c%e5%b8%ae%e5%8a%a9%e7%90%86%e8%a7%a3evaluate%e3%80%81query%e5%8f%8a%e8%8e%b7%e5%8f%963%e7%a7%8d%e7%bb%93%e6%9e%9c%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.zhaozhao123.cn\/php\/post\/42.html","title":{"rendered":"PHP DOMXPath\u4f7f\u7528\u793a\u4f8b\uff0c\u5e2e\u52a9\u7406\u89e3evaluate\u3001query\u53ca\u83b7\u53d63\u79cd\u7ed3\u679c\u65b9\u5f0f"},"content":{"rendered":"\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n$html = &lt;&lt;&lt;HTML\n&lt;article>\n    &lt;h2>Article Title&lt;\/h2>\n    &lt;div class=\"entry-content\">\u4f60\u597d\n        &lt;p>This is the content of the article.&lt;\/p>\n        &lt;p>This is the content of the article.&lt;\/p>\n        &lt;p>This is the content of the &lt;span>article&lt;\/span>.&lt;\/p>\n    &lt;\/div>\n&lt;\/article>\nHTML;\n\n$dom = new DOMDocument();\n@$dom->loadHTML($html);\n$xpath = new DOMXPath($dom);\n\n$title = $xpath->evaluate('string(<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2gy\" rel=\"noopener noreferrer nofollow\">\/\/h2<\/a>');\n\n\/\/ \u67e5\u8be2 div \u5143\u7d20\n$contentNode = $xpath->query('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2Rpdg==\" rel=\"noopener noreferrer nofollow\">\/\/div<\/a>&#91;@class=\"entry-content\"]')->item(0);\n\n\/\/ \u521d\u59cb\u5316\u5185\u5bb9\u5b57\u7b26\u4e32\n$content = '';\n\n\/\/ \u68c0\u67e5\u662f\u5426\u627e\u5230\u4e86\u76ee\u6807\u8282\u70b9\nif ($contentNode) {\n    \/\/ \u904d\u5386\u76ee\u6807\u8282\u70b9\u7684\u6240\u6709\u5b50\u8282\u70b9\n    foreach ($contentNode->childNodes as $childNode) {\n        \/\/ \u5c06\u6bcf\u4e2a\u5b50\u8282\u70b9\u4fdd\u5b58\u4e3a HTML \u5b57\u7b26\u4e32\u5e76\u62fc\u63a5\u5230\u5185\u5bb9\u4e2d\n        $content .= $dom->saveHTML($childNode);\n    }\n}\n\n$contentNode2 = $xpath->query('<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2Rpdg==\" rel=\"noopener noreferrer nofollow\">\/\/div<\/a>&#91;@class=\"entry-content\"]')->item(0);\n\/\/ \u83b7\u53d6\u8be5\u8282\u70b9\u53ca\u5176\u5b50\u8282\u70b9\u7684 HTML \u5185\u5bb9\nif ($contentNode2) {\n    $content2 = $dom->saveHTML($contentNode2);\n} else {\n    $content2 = '';\n}\n\n$content3 = $xpath->evaluate('string(<a href=\"https:\/\/www.zhaozhao123.cn\/skin\/go?url=aHR0cDovL2Rpdg==\" rel=\"noopener noreferrer nofollow\">\/\/div<\/a>&#91;@class=\"entry-content\"])');\n\necho \"Title: $titlen\"; \necho \"Content: $contentn\";\necho \"Content: $content2n\";\necho \"Content: $content3n\";\n?><\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Title: Article Title\n\nContent: \u4f60\u597d\n&lt;p>This is the content of the article.&lt;\/p>\n&lt;p>This is the content of the article.&lt;\/p>\n&lt;p>This is the content of the &lt;span>article&lt;\/span>.&lt;\/p>\n\nContent2: &lt;div class=\"entry-content\">\u4f60\u597d\n&lt;p>This is the content of the article.&lt;\/p>\n&lt;p>This is the content of the article.&lt;\/p>\n&lt;p>This is the content of the &lt;span>article&lt;\/span>.&lt;\/p>\n&lt;\/div>\n\nContent3: \u4f60\u597d\nThis is the content of the article.\nThis is the content of the article.\nThis is the content of the article.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u793a\u4f8b\u4ee3\u7801\uff1a \u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"tuisongtax":[],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-jswz"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":0,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=42"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}