Using PHP Replace SPACES in URLS with

后端 未结 6 1881
太阳男子
太阳男子 2020-11-27 18:46

I\'m looking to replace all instances of spaces in urls with %20. How would I do that with regex?

Thank you!

6条回答
  •  一整个雨季
    2020-11-27 19:33

    I think you must use rawurlencode() instead urlencode() for your purpose.

    sample

    $image = 'some images.jpg';
    $url   = 'http://example.com/'
    

    With urlencode($str) will result

    echo $url.urlencode($image); //http://example.com/some+images.jpg
    

    its not change to %20 at all

    but with rawurlencode($image) will produce

    echo $url.rawurlencode(basename($image)); //http://example.com/some%20images.jpg
    

提交回复
热议问题