[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP: urlencode everything after last slash

Posted on 2011-05-05
6
Medium Priority
?
798 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:hankknight
  • 3
  • 2
6 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35699159
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
 
LVL 82

Expert Comment

by:hielo
ID: 35699205
try:
function urlEncodeAfterSlash ($str) {
return preg_replace('#(.*)([^/]+)$#e',"'\\1'.urlencode('\\2')",$str);
}
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 total points
ID: 35699248
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:hielo
ID: 35699278
rawurlencode() should change the spaces to %20:

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

Author Comment

by:hankknight
ID: 35699518
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
 
LVL 82

Accepted Solution

by:
hielo earned 1400 total points
ID: 35700255
return preg_replace('#([^/]*/)([^/]+)$#e',"'\\1'.rawurlencode('\\2')",$str);
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question