You can find the last "/" and use urlencode for the last part of your line, for example:
$pos=strrpos($url,"/")+1; $newurl=substr($url,0,$pos) . rawurlencode(substr($url,$pos));
If your only problems are spaces, you can use
str_replace(" ","%20",$url);
source share