如何使特殊字符与mailto:link(PHP)兼容?

I'm trying to put html into a mailto link (<a href='mailto:...) and using htmlescapechars() but this doesn't seem to be what I need.

<a href='mailto:?subject=<?echo $subject_e=htmlspecialchars($subject, ENT_QUOTES)?>&body=<?=$subject_e?>%20via%20https&#58;&#47;&#47;mySite.com&#47;<?=htmlspecialchars($subjectLink, ENT_QUOTES)?>%20'>some text</a>

Can anyone explain under what conditions Example #1 in the htmlspecialchars() part of PHP's documentation works?

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;

Running this function on my own server or on http://writecodeonline.com/php/ doesn't seem to convert the special characters.

So it doesn't appear to be a PHP version issue. Including the encoding parameter also had no effect. Wtf?

So after looking elsewhere on SO, I realized instead of htmlspecialchars() I just needed rawurlencode().

Running this function on my own server [...] doesn't seem to convert the special characters.

I bet you’re just thinking that because you view the script output as HTML … and what does &lt; display as in HTML? Right, a < character …

It actually IS working. If you view the source, you will see it is working properly. The HTML is rendering the special chars though

If you look at the code of your page you'll see what was generated was exactly what was expected. Loo:

     <div class="page-body">
        <div id="code-output" class="code-output">&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;</div>
     </div>

The problem is that when you render the page, the html entities are converted to text again. If it's not what you want, you should put the output inside <pre> tags.

You're looking at it wrong! The reason you're seeing the HTML tags IS because the magic php function is working. Otherwise, it would just render as actual HTML. This is happening because you're likely printing on to an HTML page.