Question

I have a method that is returning those typea of strings:

string(6) "<math>"
string(12) " <semantics>"
string(8) "  <mrow>"
string(29) "   <mi>A</mi><mrow><mo>(</mo>"
string(14) "    <mi>T</mi>"
string(27) "   <mo>)</mo></mrow></mrow>"
string(13) " </semantics>"
string(7) "</math>"

My goal is to append this to a DOMDocument. Is there a possibility to check if I have a tag in my string, so the tag should be added as child, not node value.

Thank you in advance.

Was it helpful?

Solution

I think this might help you

//I assume the name of your method is "getStringsNodes()" and it returns the array of strings
$string_array = getStringNodes();
$string_array = array_map('trim', $string_array);
$string_xml = implode('', $string_array);
$new_nodes = new DomDocument();
$new_nodes->loadXML($string_xml);
$first_node = $new_node->getElementsByTagName('math')->item(0);

//I assume the parent XML container is $owner_xml
$owner_xml = new DomDocument();
$owner_xml->loadXML('<sample><parent_node></parent_node></sample>'); //convert this to whatever you need
$node = $owner_xml->importNode($first_node, true);
$parent_node = $owner_xml->getElementsByTagName('parent_node')->item(0);
$parent_node->appendChild($node);

I hope this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top