Forum Moderators: coopster

Message Too Old, No Replies

Trouble with percent sign as %20 in URL

Might be able to fix with regex hack or mod_rewrite

         

member123

6:46 pm on May 9, 2008 (gmt 0)

10+ Year Member



I'm currently trying to add some functionality to Feed on Feeds, an RSS reader that uses PHP/MySQL. The reader includes an autotagger that tags posts as they come in for some predefined tags that you input. However, if I use a multi-word tag that includes a space, things get all screwed up because the server encodes the space as %20.

Right now, when you filter by a tag, it goes to this URL:
http://example.com/b/?what=Youtube and it displays all posts with a YouTube tag.
If you filter by a multiword tag, it goes to something like this:
http://example.com/b/?what=Steve%20Jobs and tries to display all the tags with a Steve%20Jobs tag. None show up, because in the database they are tagged as Steve Jobs.

I came up with this code to try to replace the %20 with a space, but so far it hasn't worked:

if(!isset($_GET['what']))
{
$what = "unread";
} elseif(preg_match("/%[20]/", $what))
{
$what = preg_replace("/%[20]/"," ",$name2);
}
else
{
$what = $_GET['what'];
}

Does anybody see what I'm doing wrong, or have any other ideas about what do to about this? I think there might be a way to fix it using mod_rewrite, but I've never worked with that before so I'm not sure where to start.

I'm sure this is a pretty common problem, so hopefully you guys can help me. Thanks.

[edited by: coopster at 6:50 pm (utc) on May 9, 2008]
[edit reason] used example.com for domain [/edit]

coopster

6:53 pm on May 9, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, member123.

Why not just use the PHP function to urldecode [php.net] it?

PHP_Chimp

6:56 pm on May 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"/%[20]/" means a character class of either a 2 or a 0, so this doesnt match your %20. You could use /%20/ to match that.

However how about urldecode [php.net]? As this should change your encoded space into the real thing to allow you to show the correct page from your database.

<edit>
Beaten to it ;)

[edited by: PHP_Chimp at 6:57 pm (utc) on May 9, 2008]

member123

7:12 pm on May 9, 2008 (gmt 0)

10+ Year Member



@coopster: That was one of the things that I tried. It didn't work, but I might have been doing it in the wrong place.

When I did it, it looked like this:
if(!isset($_GET['what']))
{
$what = "unread";
}
else
{
$what = urldecode($_GET['what']);
}

Perhaps I was doing something wrong?

member123

1:09 pm on May 10, 2008 (gmt 0)

10+ Year Member



Update:

I tried changing the code to this, just to see if the problem was something else:

if(!isset($_GET['what']))
{
$what = "unread";
}
else
{
$what = "Steve Jobs";
}

Even that code doesn't load the tags for "Steve Jobs". I checked the database, and the tag appears in the database as "Steve Jobs" without the quotes.

Does anybody know at what point the space is being replaced, so that I can do something about it there?

Thanks.

coopster

2:49 pm on May 12, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



At what point the space is being replaced? Are you building links? If you are pulling the value from the database table as "Steve Jobs" and are trying to make a link out of it, try using urlencode [php.net].

member123

8:48 pm on May 12, 2008 (gmt 0)

10+ Year Member



Hey Coopster, yes I'm making links out of them. I'll try that out too.