Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用XPATH检索嵌入在HTML注释中的JSON?_Xpath - Fatal编程技术网

如何使用XPATH检索嵌入在HTML注释中的JSON?

如何使用XPATH检索嵌入在HTML注释中的JSON?,xpath,Xpath,我有表单的HTML输出: <!-- wp:uagb/section {"block_id":"e00ee750-246d-46fd-a034-c6dc37497309","contenttype":"exercise","contenttitle":"here is exercise 1","contentname":"Exercise"} --> <div id="here-is-exercise-1" class="contenttype-wrapper sometopic

我有表单的HTML输出:

<!-- wp:uagb/section {"block_id":"e00ee750-246d-46fd-a034-c6dc37497309","contenttype":"exercise","contenttitle":"here is exercise 1","contentname":"Exercise"} -->
<div id="here-is-exercise-1" class="contenttype-wrapper sometopictype-exercise" data-id="e00ee750-246d-46fd-a034-c6dc37497309">
<!-- wp:paragraph -->
   <p>Some stuff</p>
<!-- /wp:paragraph -->
</div>
<!-- /wp:uagb/section -->
我正在尝试形成一个XPATH查询(在PHP中),它的形式是

"Get me all JSON objects that contain parameter `contenttype`"
对于正常的DOM提取,我非常精通XPATH,但不太确定如何进行。想法?

好的,根据这篇文章

以下是我的想法:

$contenttypes = $xpath->query("//comment()[contains(.,'contenttype')]");
foreach ($contenttypes as $contenttype) {
    preg_match_all("/\\{(.*?)\\}/", $contenttype->data, $matches);
    $data = json_decode(trim($matches[0][0], '"')); 
然后我需要的数据在$data变量中。没有意识到
xpath
内置了对注释的支持


我确信有人有办法在一个xpath查询中获得JSON对象。我无法理解,因此只需使用
preg\u match\u all

您想要的输出是完整的第一条注释还是仅仅是“exercise”一词?JSON对象是您需要的,然后答案第一行中的xpath表达式(
//comment()[包含(,'contenttype')]
)应该可以工作,为什么需要其余的呢?如果我这样调用xpath,它不会在返回的输出中返回文本“wp:uagb/section”吗?不,只是json注释。
$contenttypes = $xpath->query("//comment()[contains(.,'contenttype')]");
foreach ($contenttypes as $contenttype) {
    preg_match_all("/\\{(.*?)\\}/", $contenttype->data, $matches);
    $data = json_decode(trim($matches[0][0], '"'));