0

Here is my code:

       $xpath_query = $xpath->query('//div[@class="paragraph-content"]//h2[1]');
       $i = 0;
       foreach($xpath_query as $header){ 
          if (!empty($header->nodeValue) && $i == 0){
            $replacement  = $dom->createDocumentFragment();
            $replacement  ->appendXML("<h1>$header->nodeValue</h1>");
            $header->parentNode->replaceChild($replacement  , $header);
          }  
          $i++;
       }

I'm grabbing the <h2> tag in the xpath and replacing it with <h1>

Where there is a value like this "Dialed in to hike & trail" in the <h2> tag and its being replaced to <h1> tag it doesn't show the text.

the "&" or &amp is causing the replacement to break, the $header->nodeValue

how can I escape $header->nodeValue, meaning if there are cases in the string where I have a <br> tag or &amp. When I have a <br> tag or have & in the text, it breaks the replacement.

2 Answers 2

1

You may need to ensure that the entities are encoded when you create the HTML (I've used htmlentities())...

$replacement  ->appendXML("<h1>".htmlentities($header->nodeValue)."</h1>");
Sign up to request clarification or add additional context in comments.

Comments

0

I have resolved the issue by using this :

$replacement  ->appendXML("<h1>".htmlspecialchars($header->nodeValue)."</h1>");

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.