Try the following:
$str = 'http://www.example.com/some_folder/some file [that] needs "to" be (encoded).zip'; $pos = strrpos($str, '/') + 1; $result = substr($str, 0, $pos) . urlencode(substr($str, $pos));
You are looking for the last slash occurrence. The part before this is OK, so just copy this. And urlencode rest.
ducin source share