Search code examples
phpmailto

urlencode() strings for mailto:subject= with dash characters that can be properly decoded


I have a string, for example "2013 International Truck – XZ1234" which I need to urlencode() and include in a mailto subject= in a way that will be properly decoded. I have tested this in the following way:

$encodedstring = urlencode("mailto:[email protected]?subject=2013 International Truck – XZ1234")
<a href="<?php $encodedstring ?>">Email Us</a>

The link will then look like this:

<a href="mailto:[email protected]?subject=2013+International+Truck+%26%238211%3B+XZ1234">Email Us</a>

The subject then is decoded and displayed in the subject input field to look like this: (this has been tested in Gmail)

2013 International TerraStar Truck &#8211; NT2031

Is there an alternative to urlencode() or some other way to encode this string so that all characters, including dashes are properly decoded in mail clients?


Solution

  • Your string is being double encoded. %26%238211%3B is the encoded form of &#8211;, which is the encoded form of the dash. Trace your code execution to see where it is being encoded twice.