Jump to content

Replace spaces in hyperlink with %20


LostAngel

Recommended Posts

Hello, I have a script that uploads a file to my webhost, then displays a mailto link that automatically places the link in a new message. The code is below:

 

echo "<a href=\"mailto:?body=$urlvar\">Click here to open new email message with link in the body.</a>";

 

$urlvar is the php variable location of my file uploaded. Is there a way I can have the $urlvar replace any spaces with %20? In outlook, you cannot click a hyperlink that has spaces...it has to be replaced with %20.

Link to comment
Share on other sites

Can you post the part of your script that is doing this? It sounds like you are not properly quoting the output.

 

You can check this by doing a "show source" on the generated source.

 

Ken

Below is the upload script part:

if ($action == 'upload')
{

if ($_FILES["filename"]["size"] > $max_size) die ("<b>File too big!  Try again...</b>");
mkdir("./uploads/".$rand_folder);
copy($_FILES["filename"]["tmp_name"],"./uploads/".$rand_folder."/".$_FILES["filename"]["name"]) or die("<b>Unknown error!</b>");
echo "<b>File Uploaded. </b>"; // for debug -->  $filename --> ".$destination."/".$filename_name."</h2>";
echo "http://www.site.com/uploads/".$rand_folder."/".$filename_name."</br>";
$urlvar = "http://www.site.com/uploads/".$rand_folder."/".$filename_name;
$urlvar = rawurlencode($urlvar);

echo "<a href=\"$urlvar\">Right Click here and Copy Shortcut</a></br>";
echo "<a href=\"mailto:?body=$urlvar\">Click here to open new email message with link in the body.</a>";
}

?>

Link to comment
Share on other sites

by the way, this is the mailto link that is created:

 

<a href="mailto:?body=http://www.website.com/uploads/Dzi1NLoOBIUhWtd/c6 dyno predator before and after.jpg">Click here to open new email message with link in the body.</a>

 

But like I said, the hyperlink displayed in outlook cuts off right after C6, but it does display the whole link...just only underlines up to C6 for a link.

Link to comment
Share on other sites

I took the URL you posted and created the following test script:

<?php
$str = 'http://www.website.com/uploads/Dzi1NLoOBIUhWtd/c6 dyno predator before and after.jpg';
$ustr = rawurlencode($str);
echo '<pre>' . $str . "\n" . $ustr . '</pre>'."\n";
?>
<a href="mailto:?body=<?php echo $ustr;?>">Click here to open new email message with link in the body.</a>

 

This produces:

http://www.website.com/uploads/Dzi1NLoOBIUhWtd/c6 dyno predator before and after.jpg
http%3A%2F%2Fwww.website.com%2Fuploads%2FDzi1NLoOBIUhWtd%2Fc6%20dyno%20predator%20before%20and%20after.jpg

Click here to open new email message with link in the body.

 

The generated source shows:

<pre>http://www.website.com/uploads/Dzi1NLoOBIUhWtd/c6 dyno predator before and after.jpg
http%3A%2F%2Fwww.website.com%2Fuploads%2FDzi1NLoOBIUhWtd%2Fc6%20dyno%20predator%20before%20and%20after.jpg</pre>
<a href="mailto:?body=http%3A%2F%2Fwww.website.com%2Fuploads%2FDzi1NLoOBIUhWtd%2Fc6%20dyno%20predator%20before%20and%20after.jpg">Click here to open new email message with link in the body.</a>

 

This tells me that if you have coded your script correctly, it should be working.  Looking at what you posted, it doesn't look like you are outputting the encoded string.

 

Ken

Link to comment
Share on other sites

I took the URL you posted and created the following test script:

Ken, this is the code I have for displaying the url:

$urlvar = "http://www.website.com/uploads/".$rand_folder."/".$filename_name;
$urlvar = rawurlencode($urlvar);

echo "<a href=\"mailto:?body=$urlvar\">Click here to open new email message with link in the body.</a>";

Link to comment
Share on other sites

If you do a "show source" on the generated source, what do you see?

 

I tested your code and it works fine on my system, but I don't use outlook for my email client.

 

Ken

I'm thinking it must be an outlook thing, as it doesn't do it in thunderbird.

Link to comment
Share on other sites

If you do a "show source" on the generated source, what do you see?

 

I tested your code and it works fine on my system, but I don't use outlook for my email client.

 

Ken

I see:

<a href="mailto:?body=http%3A%2F%2Fwww.dadsftp.com%2Fuploads%2FLO4Mc4hygSMkCso%2Fc6%20dyno%20predator%20before%20and%20after.jpg">Click here to open new email message with link in the body.</a>

Link to comment
Share on other sites

<?
$string='http://www.website.com/uploads/Dzi1NLoOBIUhWtd/c6 dyno predator before and after.jpg';
$explode = explode(" ", $string);
$string_rw = '';

for($i=0; $i<count($explode); $i++){
if($i>0){
  $string_rw .= "%20";
}
  $string_rw .= $explode[$i];

}
$new_string = $string_rw;
echo $new_string;
?>

Link to comment
Share on other sites

  • 1 year later...

Although the post started about replacing spaces with %20, that is not the issue here. If a URL has spaces in it when received by Outlook then it breaks the hpyerlink at the space. By adding the <> either end of the URL this stops Outlook breaking it at the spaces, unless it starts a new line.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.