string with & problem

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
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

string with & problem

Post by gurjit »

I pass a variable in which can have an & in it and it causes problems because the _GET[''] sees this as a new paramater name.

how can I avoid this

so if i have the following:

http://www.mydomain.com/search/index.php?ag_name=A & E Services&cat=9

the problem here is that A & E cuts and only A is passed through
madan koshti
Forum Commoner
Posts: 50
Joined: Fri Jun 06, 2008 4:25 am

Re: string with & problem

Post by madan koshti »

Refer urlencode() php function.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: string with & problem

Post by Christopher »

Code: Select all

$ag_name = 'A & E Services';
$cat = 9;
$url = 'http://www.mydomain.com/search/index.php?ag_name=' . urlencode($ag_name) . '&cat=' . urlencode($cat);
(#10850)
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Re: string with & problem

Post by gurjit »

I tried this but it never worked.

this is how i pass the the url through my .htaccess file

Code: Select all

 
<a href="http://www.mydomain.co.uk/8630/uk/england/a-&-e-estates-ltd/a-&-e-estates-ltd-estate-agent-list.html">click</a>
 
the .htaccess passes the 'a-&-e-estates-ltd' string to my search/index.php?searchtext=a-&-e-estates-ltd but only looks for 'a' and not 'a-&-e-estates-ltd'

so heres what I do to my string before I pass it into the query

simply replaces the - (dashes) with spaces and then makes sure only one space exists between a word.

The next stage is to manipulate any text if an & (amp) exists in the _GET['searchtext'] variable. How do I go about doing this?

Code: Select all

 
<?php
$_GET['searchtext'] = str_replace("-"," ",$_GET['searchtext']);
$_GET['searchtext'] = ereg_replace( '  +', ' ', $_GET['searchtext']);
?>
 
LSJason
Forum Commoner
Posts: 45
Joined: Mon May 12, 2008 4:43 pm

Re: string with & problem

Post by LSJason »

You need to escape the ampersand.
Post Reply