Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 595
user3204729 Avatar asked Nov 24 '22 05:11

user3204729


1 Answers

A safer approach in my case was to use:

$dom->saveXML();
like image 146
Ivan Chaer Avatar answered Dec 21 '22 01:12

Ivan Chaer