1

I have a text input within a form that uses the get method:

<form method="get" action="./" role="search">
  <input name="query" type="text" value="">
  <button>Go</button>
</form>

When the form is submitted with 'foo bar', the reloaded URL looks like this:

mywpsite.com/?query=foo%2Bbar

How can this be rendered with + in place of %2B as on Duckduckgo.com, Google and similar sites? Example:

mywpsite.com/?query=foo+bar
user2726041
  • 359
  • 4
  • 20
  • Str_replace? Did you try it? – Andreas Oct 27 '18 at 11:00
  • @Andreas where in the form would I use it? – user2726041 Oct 27 '18 at 11:03
  • 1
    `echo urldecode($_GET['query'])` to `echo str_replace("%2B", "+",urldecode($_GET['query']))` or `echo str_replace(" ", "+", $_GET['query'])` because urlencode makes a space `%2B` so without urlencode you can just replace space with `+`. But that could make other url characters be messed up. But what is the point of all this in the first place? – Andreas Oct 27 '18 at 11:05
  • The string gets url-encoded when you submit the form, and cannot be changed in the url itself. – Qirel Oct 27 '18 at 11:10
  • @Qirel how do sites like duckduckgo.com achieve it? – user2726041 Oct 27 '18 at 11:14
  • It's indeed browser behaviour and Firefox behaves as expected. What browser are you testing this with ? [This](https://stackoverflow.com/questions/1634271#answer-1634293) might be relevant. – msg Oct 27 '18 at 11:23
  • @msg in Safari and Chrome – user2726041 Oct 27 '18 at 11:26

1 Answers1

0

it's not a good solution, but i suppose this would work:

if(false!==stripos($_SERVER['REQUEST_URI'],"%2B")){
    http_response_code(307);
    header("Location: ".strtr($_SERVER['REQUEST_URI'],array('%2b'=>'+','%2B'=>'+')));
    die();
}
  • the code must run before the response body has been generated/sent.

this code checks if the request url contains "%2b", and if it does, tell the browser to try the request again at the url with "%2b" converted to "+".

hanshenrik
  • 19,904
  • 4
  • 43
  • 89