Help with removing &'s

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
303tech
Forum Commoner
Posts: 31
Joined: Sun Mar 11, 2007 3:25 pm

Help with removing &'s

Post by 303tech »

I'm having problems when the string has &'s in it, as well as 's. I was able to remove the 's. But i get a return value of 0 when trying to do the same thing with &'s. If i take out the part where I'm trying to remove &, the value of $replace is OK.

Code: Select all

//PARSE INI FILE.
$ini = parse_ini_file('config.ini');

//REMOVE APOSTROPHES FROM NAME.
$and="&";
$ini1=$ini['UNITNAME'];
$unitname=str_replace($and,"AND",$ini1);
echo $unitname;
$replace=str_replace("'","",$unitname);
echo $replace;
Also, is there a way that I can put chars like this in db fields?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Code: Select all

$and = "&";
$speech = "'";

$ini['UNITNAME'] = "AND & SPEECH '";
$ini1 = $ini['UNITNAME'];

$unitname = str_replace($and,"AND",$ini1);
$unitname = str_replace($speech, "APOSTROPHY", $unitname);
echo $unitname;
That works fine for me.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Have you checked that $ini['UNITNAME'] was set to begin with?
303tech
Forum Commoner
Posts: 31
Joined: Sun Mar 11, 2007 3:25 pm

Post by 303tech »

in the ini...i think its having a problem because it reads .

UNITNAME=Joe's Pizza & Pasta

but returns a 0 still.

I take off the part where im trying to take out the &, and it returns fine with just the apostrophe taken out.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Help with removing &'s

Post by Kieran Huggins »

303tech wrote:I'm having problems...
Can you be more specific?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

str_replace(array("'", "&"), '', $var);
that, or a modification of it, should work
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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Also, is there a way that I can put chars like this in db fields?
mysql_real_escape_string()
htmlentities()
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.
303tech
Forum Commoner
Posts: 31
Joined: Sun Mar 11, 2007 3:25 pm

Post by 303tech »

PHP PARSE Actually returns 0 when there is a & in the string. There is nothing to parse. *scratches head*
Post Reply