title: DOMDocument->saveHTML() vs urlencode with commercial at symbol (@)
body: <p>Using <a href="http://php.net/manual/en/class.domdocument.php"><code>DOMDocument()</code></a>, I'm replacing links in a <code>$message</code> and adding some things, like <code>[@MERGEID]</code>. When I save the changes with <a href="http://php.net/manual/en/domdocument.savehtml.php"><code>$dom_document-&gt;saveHTML()</code></a>, the links get "sort of" url-encoded. <code>[@MERGEID]</code> becomes <code>%5B@MERGEID%5D</code>.</p>
  
  <p>Later in my code I need to replace <code>[@MERGEID]</code> with an ID. So I search for <code>urlencode('[@MERGEID]')</code> - however, <a href="http://php.net/manual/en/function.urlencode.php"><code>urlencode()</code></a> changes the commercial at symbol (@)  to %40, while saveHTML() has left it alone. So there is no match - <code>'%5B@MERGEID%5D' != '%5B%40MERGEID%5D'</code></p>
  
  <p>Now, I know can run <code>str_replace('%40', '@', urlencode('[@MERGEID]'))</code> to get what I need to locate the merge variable in $message. </p>
  
  <p><strong>My question is</strong>, what RFC spec is DOMDocument using, and why is it different than urlencode or even rawurlencode? Is there anything I can do about that to save a str_replace?</p>
  
  <p>Demo code:</p>
  
  <pre><code>$message = '&lt;a href="http://www.google.com?ref=abc" data-tag="thebottomlink"&gt;Google&lt;/a&gt;';
  $dom_document = new \DOMDocument();
  libxml_use_internal_errors(true); //Supress content errors
  $dom_document-&gt;loadHTML(mb_convert_encoding($message, 'HTML-ENTITIES', 'UTF-8'));       
  $elements = $dom_document-&gt;getElementsByTagName('a');
  foreach($elements as $element) {    
      $link = $element-&gt;getAttribute('href'); //http://www.google.com?ref=abc
      $tag = $element-&gt;getAttribute('data-tag'); //thebottomlink
      if ($link) {
          $newlink = 'http://www.example.com/click/[@MERGEID]?url=' . $link;
          if ($tag) {
              $newlink .= '&amp;tag=' . $tag;
          } 
          $element-&gt;setAttribute('href', $newlink);
      }
  }
  $message = $dom_document-&gt;saveHTML();
  $urlencodedmerge = urlencode('[@MERGEID]');
  die($message . ' and url encoded version: ' . $urlencodedmerge); 
  //&lt;a data-tag="thebottomlink" href="http://www.example.com/click/%5B@MERGEID%5D?url=http://www.google.com?ref=abc&amp;amp;tag=thebottomlink"&gt;Google&lt;/a&gt; and url encoded version: %5B%40MERGEID%5D
  </code></pre>