rawurlencode()
is what you're looking for. However, if your Content-Type
is set to text/html
(which is the default), then you will see the space character instead of the encoded entity.
header('Content-Type: text/plain');
$str = "my string";
echo rawurlencode($str); // => my%20string
Note: I'm not suggesting that you should change the Content-Type
header in your original script. It's just to show that your rawurlencode()
call is working and to explain why you're not seeing it.