PHP replace spaces in urls image name

Looking to replace all instances of spaces in urls with %20? No need for a regex. Use rawurlencode() instead urlencode() for this purpose.

$img = 'your image.jpg';
$site   = ''
$url   = $site.$img

urlencode($img) will result

echo $site.urlencode($img);

It will not change to %20.

But with rawurlencode($img), it produce

echo $site.rawurlencode($img);

Another way of doing it is using str_replace

$url = $site.str_replace(' ', '%20', $img);

By Keenlio, July 21, 2014

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × = twelve

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>