Search code examples
phpstrip-tags

Display content from a specific tag only and ignore others


I have a Custom Tag Generator system for XML. I am using custom PHP. Here I am able to add custom XML tags and add its functionality. What I want to achieve is that I would like to show display contents from a specific tag only. Check out the following code if mine:

<?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>

From the above code I would like to show "Testing Title" only but not anything else.

So far I am using the following code without having any luck.

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

Thanks in advance.


Solution

  • PHP has several builtin XML libraries. For this use case I would opt for SimpleXML:

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

    (demo)