LostAngel Posted July 14, 2008 Share Posted July 14, 2008 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 https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/ Share on other sites More sharing options...
waynew Posted July 14, 2008 Share Posted July 14, 2008 How about urlencode() ? Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589591 Share on other sites More sharing options...
LostAngel Posted July 14, 2008 Author Share Posted July 14, 2008 I just tried that, and it replaced the spaces with + signs...which is not working when clicked on. Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589600 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 The function you want is rawurlencode(). Ken Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589602 Share on other sites More sharing options...
samshel Posted July 14, 2008 Share Posted July 14, 2008 try rawurlencode(); Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589605 Share on other sites More sharing options...
LostAngel Posted July 14, 2008 Author Share Posted July 14, 2008 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 Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589606 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 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 Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589607 Share on other sites More sharing options...
LostAngel Posted July 14, 2008 Author Share Posted July 14, 2008 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 https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589638 Share on other sites More sharing options...
ChadNomad Posted July 14, 2008 Share Posted July 14, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589735 Share on other sites More sharing options...
LostAngel Posted July 14, 2008 Author Share Posted July 14, 2008 I tried, but that still breaks my link. Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589740 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 DO you have a link that we can access that shows the problem? Ken Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589759 Share on other sites More sharing options...
LostAngel Posted July 14, 2008 Author Share Posted July 14, 2008 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 https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589789 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 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 https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589805 Share on other sites More sharing options...
LostAngel Posted July 14, 2008 Author Share Posted July 14, 2008 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 https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589811 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 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 Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589827 Share on other sites More sharing options...
LostAngel Posted July 14, 2008 Author Share Posted July 14, 2008 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 https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589881 Share on other sites More sharing options...
LostAngel Posted July 14, 2008 Author Share Posted July 14, 2008 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 https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589907 Share on other sites More sharing options...
alco19357 Posted July 14, 2008 Share Posted July 14, 2008 <? $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 https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589909 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 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 Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-589912 Share on other sites More sharing options...
monkeymiles Posted March 1, 2010 Share Posted March 1, 2010 Add < and > around the URL and outlook keeps the URL together ignoring the spaces. Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-1019981 Share on other sites More sharing options...
inversesoft123 Posted March 1, 2010 Share Posted March 1, 2010 $correcturl = str_replace(" ", "%20", $_POST); or $correctpostfrom = str_replace(" ", "%20", $url); Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-1019985 Share on other sites More sharing options...
monkeymiles Posted March 1, 2010 Share Posted March 1, 2010 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 https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-1019989 Share on other sites More sharing options...
monkeymiles Posted March 1, 2010 Share Posted March 1, 2010 Although this article talks about creating an email, it is the same when outlook receives one. http://office.microsoft.com/en-us/outlook/HA011272421033.aspx Link to comment https://forums.phpfreaks.com/topic/114655-replace-spaces-in-hyperlink-with-20/#findComment-1019991 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.