Take the 2-minute tour ×

I am pretty new to php domdocument and I am trying to wrap the html with a <a> tag when there is a span tag.

My html is like

text….<span>test here.</span>…another text here

I want to wrap them with php domdocument.

  $dom = new DomDocument();

        $xpath=new DOMXpath($dom);
        $result = $xpath->query('//span');

              if($result->length > 0){
                 //I am not sure what to do next……..


Thanks for the help!

share|improve this question
Is this all the HTML? Can it include many span elements or just one? –  Jon Aug 27 '13 at 19:57
this is just an example. I have many html and many of them have span tag –  FlyingCat Aug 27 '13 at 20:01

1 Answer 1

If I am understanding what you are trying to do, this post might help you: How can I wrap two chosen elements with a div using PHP's DOMDocument?

From what I can tell, you need to use appendChild() to make $result a child of $strong. That should look something like:

share|improve this answer

Your Answer


