Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Escaping a '&' symbol in a URL
Old 08-28-2010, 03:02 PM Escaping a '&' symbol in a URL
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
I have links on a page on my Wordpress website that look something like this:
Code:
directory/?category=Audio+%26+Video
I'm using +%26+ because I need the search to look for a ' & ' (i.e. an ampersand with a space on either side of it).

Obviously I can't use '&' or '&' because then the PHP code I'm using will expect a value after it.

But if I click a link such as
Code:
directory/?category=Audio+%26+Video
Then no results are displayed, even though there should be! There are many records in my database that have this value.

As a quick test, I performed a search through Wordpress' search box and only entered '&' into the text field.

After clicking search I noticed that it appeared as '%26' in the address bar, so that must be the way to search for an ampersand character... but it just doesn't bring up any results when clicking the hardcoded links!

Does anybody know what the problem might be? I hope the only solution isn't for me to go through countless Wordpress custom fields and replace every '&' symbol for the word 'and'
__________________

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 08-28-2010, 03:06 PM Re: Escaping a '&' symbol in a URL
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
urlencode
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-28-2010, 03:41 PM Re: Escaping a '&' symbol in a URL
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
Thanks for the link, but I seem to be having the same problem

I've added this code to produce a link to search for 'Travel & Tourism':
PHP Code:
<?php
                        $foo 
'Travel & Tourism';
                        
$query_string 'category=' urlencode($foo);
                        echo 
'<a href="';
                        echo 
get_bloginfo('url');
                        echo 
'/directory/?' htmlentities($query_string) . '">Travel & Tourism</a>';
                    
?>
It appears that the value of $foo is being passed to the URL successfully, as my web page displays:
Quote:
Travel & Tourism
In the <h1> tag (I've set it to output the search term). I can also see in the address bar that the '&' symbol has been converted to '%26'.

But still no results are showing

I've looked in my database and the record is saved as 'Travel & Tourism'.

Not sure why this wouldn't be brought up in the results??
__________________

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 08-28-2010, 03:59 PM Re: Escaping a '&' symbol in a URL
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You reverse the encode with urldecode before performing the search.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-28-2010, 04:20 PM Re: Escaping a '&' symbol in a URL
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
Hmmmm I've changed my search query to:
PHP Code:
<?php query_posts('post_type=company&meta_key=Category&meta_value=' htmlspecialchars(urldecode($_GET['category'])) . '&orderby=title&order=ASC&paged=' $paged); ?>
But still no luck.

I've even tried:
PHP Code:
<?php echo htmlspecialchars(urldecode($_GET['category'])); ?>
And the result is:
Quote:
Travel & Tourism
__________________

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 08-29-2010, 04:45 PM Re: Escaping a '&' symbol in a URL
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Chances are that your results are failing and Audio & Video produces no results
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-31-2010, 01:18 PM Re: Escaping a '&' symbol in a URL
Average Talker

Posts: 19
Name: Colton
Location: NC, USA
Trades: 0
Quote:
Originally Posted by pealo86 View Post
I've even tried:
PHP Code:
<?php echo htmlspecialchars(urldecode($_GET['category'])); ?>
There is no need to use htmlspecialchars(urldecode(...)). This is going to turn the & right back into &amp; when the urldecode() function is ran. This is probably why you are running into problems. Try using ONLY urlencode() and urldecode().
Colton is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Escaping a '&' symbol in a URL
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26855 seconds with 12 queries