PHP: urlencode everything after last slash

Using PHP, I want to urlencode  only the stuff after the last slash.
<?php

echo urlEncodeAfterSlash ('/my dir/one two/???/Hello World');
// should return '/my dir/one two/???/Hello%20World'

echo urlEncodeAfterSlash ('Hello World');
// should return 'Hello%20World'

function urlEncodeAfterSlash ($str) {
return $str;
}

?>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
 
hieloCommented:
return preg_replace('#([^/]*/)([^/]+)$#e',"'\\1'.rawurlencode('\\2')",$str);
0
 
Ray PaseurCommented:
use end(explode('/', $str)) to isolate the data.  Then replace the end with the urlencoded thing and implode('/') to reassemblethe string.

Is it really a slash you want or is it the directory separator?
0
 
hieloCommented:
try:
function urlEncodeAfterSlash ($str) {
return preg_replace('#(.*)([^/]+)$#e',"'\\1'.urlencode('\\2')",$str);
}
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Ray PaseurCommented:
This seems to work. Note that urlencode() does not make blanks into %20; instead it makes them into the plus sign.  You could add a separate step if you wanted to change that behavior.
http://www.laprbass.com/RAY_temp_hankknight.php
Outputs:
/my dir/one two/???/Hello+World
Hello+World
<?php // RAY_temp_hankknight.php
error_reporting(E_ALL);

echo "<br/>" . urlEncodeAfterSlash ('/my dir/one two/???/Hello World');
// should return '/my dir/one two/???/Hello%20World'

echo "<br/>" . urlEncodeAfterSlash ('Hello World');
// should return 'Hello%20World'

function urlEncodeAfterSlash ($str, $slash=DIRECTORY_SEPARATOR)
{
    if (FALSE === strpos($str, $slash)) return urlencode($str);
    $arr = explode($slash, $str);
    if ($end = array_pop($arr))
    {
        $arr[] = urlencode($end);
    }
    $str = implode($slash, $arr);
    return $str;
}

Open in new window

0
 
hieloCommented:
rawurlencode() should change the spaces to %20:

function urlEncodeAfterSlash ($str) {
return preg_replace('#(.*)([^/]+)$#e',"'\\1'.rawurlencode('\\2')",$str);
}
0
 
hankknightAuthor Commented:
Thanks.

hielo, your code does not properly encode everything for me.  Please test the code below, thanks.
<?php


echo 'This is what I want:<br />';
echo 'Hello World/sub-dir/This%20is%20a%20test%2C%20and%20only%20a%20test.';

echo '<hr />';

echo 'This does not proporly encode everything:<br />';
echo urlEncodeAfterSlash('Hello World/sub-dir/This is a test, and only a test.');

echo '<hr />';

echo 'This encodes too much, including everything before the last slash:<br />';
echo rawurlencode       ('Hello World/sub-dir/This is a test, and only a test.');


function urlEncodeAfterSlash ($str) {
// Thanks, hielo
return preg_replace('#(.*)([^/]+)$#e',"'\\1'.rawurlencode('\\2')",$str);
}


?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.