PHP: convert spaces in string into %20?

matt picture matt · Apr 6, 2011 · Viewed 103.8k times · Source

How can I convert spaces in string into %20?

Here is my attempt:

$str = "What happens here?";
echo urlencode($str);

The output is "What+happens+here%3F", so the spaces are not represented as %20.

What am I doing wrong?

Answer

Matthew Flaschen picture Matthew Flaschen · Apr 6, 2011

Use the rawurlencode function instead.