仅显示来自特定标签的内容并忽略其他内容

Display content from a specific tag only and ignore others

我有一个用于 XML 的自定义标签生成器系统。我正在使用自定义 PHP。在这里,我可以添加自定义 XML 标签并添加其功能。我想要实现的是我只想显示特定标签的显示内容。如果是我的,请查看以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<jsaonthego>
  <jobdetails>
    <templateversion>1</templateversion>
    <title>Testing Title</title>
    <taskdetails>Testing Details</taskdetails>
  </jobdetails>
<cem>
    <item>999</item>
</cem>

</jsaonthego>

根据上面的代码,我只想显示“测试标题”,而不是其他任何内容。

到目前为止,我一直在使用以下代码,但运气不佳。

<?php
$result = preg_replace('/<jsaonthego<jobdetails<templateversion<taskdetails<cem<item\b[^>]*>(.*?)<\/jsaonthego>item>taskdetails>templateversion>jobdetails>cem>/i', '', $title);
echo $result;
?>

提前致谢。

PHP 有几个内置 XML libraries. For this use case I would opt for SimpleXML:

$jsaonthego = simplexml_load_string($xml);
echo $jsaonthego->jobdetails->title;

(demo)