2

I am stuck big time on this problem and google has been of no help to me so far. I am trying to find a way to preserve white space in a URL with moderate to no luck.

I have a form that needs to gather post data, mail it, and then append the post data to the URL as comma separated value and redirects them to a page where they download a product.

Once the user presses download that page reads the data in the URL and applies it to a billing invoice (the program is billed on time usage).

A simplified example:

$addressOne = $_POST['addressOne']; $newURL = "http://subdomain.domain.com/connectnow=on?" . ", Address1=" . $addressOne; If(mailSent) { header("Location: $newURL") } 

There are a lot more values obviously, but the address is one of the areas that I am having this issue.

I have tried doing something like:

$newURL = str_replace(" ", " ", $newURL); 

That worked as far as preserving the whitespace in the URL visually, but when the program that gets downloaded reads the URL it replaces the   as %C2%.

I have also tried:

$newURL = str_replace(" ", " \40", $newURL); 

That made the spaces in the URL convert back to %20.

Any guidance would be appreciated.

6
  • 3
    Have you tried urlencode() and urldecode()? Commented Apr 15, 2015 at 18:54
  • better you should use urlencode and decode Commented Apr 15, 2015 at 18:55
  • stackoverflow.com/questions/497908/… Commented Apr 15, 2015 at 18:55
  • Space and special characters are URL encoded while in browser if you read url in the redirected page using PHP you will get space back. It is normal behavior. Commented Apr 15, 2015 at 18:56
  • 1
    The reason you can't have literal whitespace in the path is that a HTTP request line has to be serialized as GET␣/virt/path?params␣HTTP/1.1. Extra spaces there would invalidate it. Which is why browsers substitute + or %20 right away. -- Also please don't copy sample code from Word etc. Those typographic quotes make poor examples for any other users that accidentally come across this question. Commented Apr 15, 2015 at 18:58

1 Answer 1

2

URL:

www.site.com/my spaces preserved/

urlencode()

www.site.com%2Fmy+spaces+preserved%2F 

urldecode()

www.site.com/my spaces preserved/ 
Sign up to request clarification or add additional context in comments.

3 Comments

I tried urlencode() previously and didn't mention it. I just tried the urldecode() and it didn't work either. I know that it is possible because this is a redesign of a site that was originally done (by someone else) five years ago using coldfusion.
what did you got (output) when trying the urldecode()?
www.site.com/my%20space%20preserved/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.