Skip to content

Instantly share code, notes, and snippets.

@peterfox
Last active December 18, 2015 09:49
Show Gist options
  • Save peterfox/5764283 to your computer and use it in GitHub Desktop.
Save peterfox/5764283 to your computer and use it in GitHub Desktop.
url encoding a mail to link in PHP
<?php
$URL = "mailto:?subject=Is this a test message!";
$url_var = $URL;
//explode it and if it has a '?' it means it must have URL Parameters
if(count($mail_split = explode('?', $URL))>1)
{
//if for some reason there's more than one '?' then we'll just add the exploded parts to the second part
$mail_params = $mail_split[1];
for($i=2; $i<count($mail_split); $i++)
{
$mail_params .= '?'.$mail_split[$i];
}
$final_mail_params = array();
//break up the parameters into the $output array
parse_str($mail_params, $output);
foreach($output as $key => &$value)
{
//reform each key value pair but with a now url encoded value
$final_mail_params[] = $key.'='.urlencode($value);
}
//make the string with all the key value pairs joined by &
$final_mail_params_string = implode('&', $final_mail_params);
//stick the original start of the mailto url back with the new encoded parameters
$url_var = $mail_split[0].'?'.$final_mail_params_string;
}
echo $url_var."\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment