urlencode($title) problem when w/ is encountered

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simplyi
Forum Newbie
Posts: 10
Joined: Fri Dec 11, 2009 4:55 pm

urlencode($title) problem when w/ is encountered

Post by simplyi »

Hello!

I have come accross a problem when urlencode retuns not expected result. Please advise me what function to use that correctly encodes URL when characters like w/ are met?

Current usage: urlencode($title)

where $title = Small Town DJs w/ Kenzie Clarke

Expected result:
Small+Town+DJs+w%26+Kenzie+Clarke

Returned result:
Small+Town+DJs+w%26%2347+Kenzie+Clarke

Thank you very much and I am looking for your advises and support.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: urlencode($title) problem when w/ is encountered

Post by requinix »

Actually the result you're expecting is with %2F not %26.

Are you sure it's not producing

Code: Select all

Small+Town+DJs+w%26%2347%3B+Kenzie+Clarke
because the HTML entity for a / is /, so

Code: Select all

$title = "Small Town DJs w/ Kenzie Clarke";
echo $title; // you see "Small Town DJs w/ Kenzie Clarke"
echo urlencode($title); // "Small+Town+DJs+w%26%2347%3B+Kenzie+Clarke"
simplyi
Forum Newbie
Posts: 10
Joined: Fri Dec 11, 2009 4:55 pm

Re: urlencode($title) problem when w/ is encountered

Post by simplyi »

Thank you very much for your reply.

The problem is that when below urlencoded title:

$title = "Small+Town+DJs+w%26%2347%3B+Kenzie+Clarke"

is used in URL like:

events/Victoria/Small+Town+DJs+w%26%2347%+Kenzie+Clarke/190

then mod_rewrite

RewriteRule ^events/([^/]+)/([^/]+)/([^/]+) events/viewEvent.php?param_1=$1&param_2=$2&param_3=$3 [NC]

parses the URL differently. One / which is %26%2347% is extra!

....

so if I urlencode this - "Small Town DJs Kenzie Clarke:" I get this

"Small+Town+DJs+Kenzie+Clarke"

but if my string contains w/ then if I urlencode "Small Town DJs w/ Kenzie Clarke"

I get

Small+Town+DJs+w%26%2347%+Kenzie+Clarke which is if used in url

events/Victoria/Small+Town+DJs+w%26%2347%+Kenzie+Clarke/190 produces an error.

I try to $eventTitle = str_replace("/","&#47",$eventTitle);

but it does not help when w/ is encountered.

Any thoughts?

Thank you.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: urlencode($title) problem when w/ is encountered

Post by requinix »

Code: Select all

$eventTitle = str_replace("/","&#47",$eventTitle);
First of all, remove that. It'll only bring trouble. urlencode is all you need.

I don't see what the problem is. If the title is Small+Town+DJs+w%2F+Kenzie+Clarke then it won't interfere with the RewriteRule you posted... unless you want the slash in there, but I really doubt it.
param_2 will be "Small Town DJs w/ Kenzie Clarke" and I was going with the assumption that's what you wanted to get.
simplyi
Forum Newbie
Posts: 10
Joined: Fri Dec 11, 2009 4:55 pm

Re: urlencode($title) problem when w/ is encountered

Post by simplyi »

tasairis, thank you for your reply. You are right, I do not need extra '/' in the URL.

Because any extra '/' in the URL breaks my mod_rewrite pattern. So I want to allow users to input '/' in the Title field and City field which I then use as part of URL. But I need the '/' to be properly url encoded so that it is not in conflict with my mod_rewrite pattern.

May I ask you to advise me how to url encode the '/' so that if the $title has the '/' simbol it is properly url encoded and extra / does not appear in the URL.

The only solution I was able to find is:

$eventTitle = urlencode($title);
$eventTitle = str_replace("%2347","",$eventTitle);

Thank you.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: urlencode($title) problem when w/ is encountered

Post by requinix »

For some reason you're encoding the / twice: the first time, I don't know, and the second time through urlencode.

The first time should not happen. That code should go away. I figured it was that one str_replace line I quoted.
So

Code: Select all

// bad
$title = "Small Town DJs w/ Kenzie Clarke";
$title = str_replace("/", "&#47", $title);
$title = urlencode($title);
 
// good
$title = "Small Town DJs w/ Kenzie Clarke";
$title = urlencode($title);
simplyi
Forum Newbie
Posts: 10
Joined: Fri Dec 11, 2009 4:55 pm

Re: urlencode($title) problem when w/ is encountered

Post by simplyi »

Thank you for your reply!

but the result of this code is a $title with '/' in it. And when used in URL of REST style like mydomain.com/events/city/venue/title/454
conflicts with mod_rewrite pattern because of additional '/' simbol.

...
Lets assume that this is our template:

Template: mydomain.com/events/city/venue/title/45

Then user inputs Event Title with several '/' characters: "Welcome to our feast the funniest/best/most interesting evening

which will make the above url look like:

URL with User input: mydomain.com/events/Vancouver/City+Hall/Welcome+to+our+feast+the+funniest/best/most+interesting+ evening/454

which conflicts with mod_rewrite template because of extra '/' and breaks php script depending on that template.


Simbols - '/' need to be encoded before use in URL.
If simbol '/' is replaced with %2F then everything works fine.
but combination of 'w/' after URLENCODE produces a result with additional '%2347' which also breaks mod_rewrite pattern and I do not understand why.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: urlencode($title) problem when w/ is encountered

Post by requinix »

I don't have any problems with slashes and urlencode:

Code: Select all

echo urlencode("a / b"); // a+%2F+b
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: urlencode($title) problem when w/ is encountered

Post by s.dot »

I would just cleanse your string of any non alphanumeric characters

Code: Select all

function cleanseString($text, $lowercase=true, $strip=true, $blank='no-title', $maxlength='255', $whole_word = true)
{
    if($lowercase)  $text = strtolower($text);
    if($strip)      $text = strip_tags($text);
 
    $text = preg_replace("/[^&a-z0-9_-\s']/i",'-',$text);
    $text = str_replace(array('&',' ','\''),array(' and ','-',''),$text);
    $text = trim(preg_replace("/-{2,}/",'-',$text), "-");
    
    if(strlen($text) > $maxlength)
    {
        $text = substr($text,0,$maxlength);
        
        if($whole_word)
        {
            $text = explode('-',$text);
            $text = implode('-',array_diff($text,array(array_pop($text))));
        }
 
    }
    
    if($text == '') return $blank;
    return $text;
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
simplyi
Forum Newbie
Posts: 10
Joined: Fri Dec 11, 2009 4:55 pm

Re: urlencode($title) problem when w/ is encountered

Post by simplyi »

Tasairis, yes, you are right. If I do

.. echo urlencode(“a / b”) then it produces an expected result on the page.

But when you put this result in URL

<a href="<?php echo urlencode('a / b')?>">Test link </a>

It results in a+/+b and I do not need this extra ‘/’ in the URL. I need it to be ‘%2f’ in browser’s address bar.

Skottayy, thank you for your reply. Is it how you usually solve urlencoding problems? Is it a proper way? I browsed for a solution and until now did not find anything that would seem as a recommended/proper way of solving these kind of situations.
Post Reply