Jump to content

Archived

This topic is now archived and is closed to further replies.

LostAngel

Replace spaces in hyperlink with %20

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.

Share this post


Link to post
Share on other sites

I just tried that, and it replaced the spaces with + signs...which is not working when clicked on.

Share this post


Link to post
Share on other sites

The function you want is rawurlencode().

 

Ken

Just tried that, and it seems to cut the link off after the first space...so a link that should be:

 

www.site.com/uploads/this is a test.doc

 

turns into

 

www.site.com/uploads/this

Share this post


Link to post
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

Share this post


Link to post
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>";
}

?>

Share this post


Link to post
Share on other sites

Can't you just replace the spaces with %20 with str_replace?

 

<?php
$url = "www.site.com/uploads/this is a test.doc";

// www.site.com/uploads/this%20is%20a%20test.doc
echo str_replace(" ", "%20", $url);
?>

Share this post


Link to post
Share on other sites

DO you have a link that we can access that shows the problem?

 

Ken

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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>";

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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>

Share this post


Link to post
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;
?>

Share this post


Link to post
Share on other sites

This shows the folly of permitting spaces in a filename. If this file is being uploaded, then it would be easiest to just rename the file when storing it, replacing all spaces with underscores.

 

Ken

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×
×
  • 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.