<root>
<code>
<command>AAA BBB CCC <cmd>DDD</cmd> EEE</command>
</code>
</root>
Excepted Output:
**AAA BBB CCC DDD EEE**
Output
**AAA BBB CCC EEE DDD**
<root>
<code>
<command>AAA BBB CCC <cmd>DDD</cmd> EEE</command>
</code>
</root>
Excepted Output:
**AAA BBB CCC DDD EEE**
Output
**AAA BBB CCC EEE DDD**
Try something along these lines:
$string = " <root>
<code>
<command>AAA BBB CCC <cmd>DDD</cmd> EEE</command>
</code>
</root>
";
$doc = new DOMDocument();
$doc->loadXML($string);
$xpath = new DOMXPath($doc);
$targets = array();
$results = $xpath->query('//command//text()');
foreach ($results as $result)
{
array_push($targets, trim($result->nodeValue) ." ");
}
foreach($targets as $target)
{
echo $target;
}
Output:
AAA BBB CCC DDD EEE
Without the code it is difficult to say what your error is. So here is an example hot to do it with DOM+Xpath:
$xmlString = <<<'XML'
<root>
<code>
<command>AAA BBB CCC <cmd>DDD</cmd> EEE</command>
</code>
</root>
XML;
$document = new DOMDocument();
$document->loadXML($xmlString);
$xpath = new DOMXPath($document);
var_dump(
$xpath->evaluate('string(//command)')
);
Output:
string(19) "AAA BBB CCC DDD EEE"
//command
fetches any command
element node in the document. string(//command)
casts the first found element into a string - it returns the text content of the node.
If you have multiple command
elements you can iterate the them and read the $textContent
property.
foreach ($xpath->evaluate('//command') as $command) {
var_dump($command->textContent);
}