php domdocument loadhtml turns $ into (url encoding)%24 unexpectedly


for this php script,

$dom = new DOMDocument();
    $dom->substituteEntities =FALSE;
    $dom->loadHTML('<a href="$a?">$a</a>');
    // print_r ($dom->getElementsByTagName("a")->item(0)->getAttribute("href")); 

//the above statement show $a? correctly

    echo $dom->saveHTML();

but it returned <a href="%24a">$a</a> to the browser when a saveHTML method was called. The $ in the href attribute was turned into %24 whereas the $ in the content of the a tag remains unchanged.

I expect the output is <a href="$a">$a</a> Is there any way to do this aside from the replace method?

By the way,

  echo $dom->saveXML();

I get what I want with saveXML(); but together with an unexpected <!--xml...... Thanks




A safer approach in my case was to use:




You can wrap echo in urldecode to solve this issue :

echo urldecode($dom->saveHTML());

People are also looking for solutions to the problem: javascript - Function runs even thou it's not called


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.