1

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.

3
  • Do mean convert them all to an HTML node and append them to an DOMDocument node? Commented Mar 14, 2014 at 14:01
  • Right, I just got an idea: put them in a single striing and use the domdocument loadhtml Commented Mar 14, 2014 at 14:02
  • Good, I am preparing a solution for you; could you tell me are you calling your method in loop and each time it returns one of the strings or it just return an array of strings? Commented Mar 14, 2014 at 14:20

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.