str_replace and & (amp;)

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
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

str_replace and & (amp;)

Post by robster »

Hello,

I'm trying to do a string replace on text in a variable called $mastercategory.
Let's say $mastercategory contains 'Blouses & Shirts'.

I run the following code:
$new_mastercategory = str_replace('&', 'and', $mastercategory);

This gives $new_mastercategory a result of:
Blouses & Shirts

I can't for the life of me figure out whey the & is in there?!

Can anyone give some advice? I'd really appreciate it, ta!

:)
Last edited by robster on Sun Feb 27, 2005 8:07 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

huh? you're str_replacing & with & .. and you wonder why & not appears in the new string? what are you smoking?
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

sorry, that was a typo, the code is actually

$new_mastercategory = str_replace("&", "and", $mastercategory);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

html_entity_decode()
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

I tried that, I used this code (this is the start of my loop thru the database)...

Code: Select all

$ShowMax = mysql_num_rows($content);	
	for ($y=1; $y<=$ShowMax; $y++)
	&#123; 	
		$id = $Xcontent&#1111;"id"];
		$mastercategory = $Xcontent&#1111;"mastercategory"];
		$new_mastercategory = str_replace("&", "and", $mastercategory);
		$new_mastercategory = html_entity_decode($new_mastercategory);
... but it still gives the exact same result. I am really getting confused. I thought, even if it did work, the html_entity_decode would just make the ampersand appear again as it is appearing as amp; in the output. What I'm trying to do is simply replace the & with and.

I am really not very advanced with php and I am sure I'm missing something, just a push in the right direction would be appreciated.

Thanks :)
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

I've also noticed, running the code:
$new_mastercategory = str_replace("&", "and", $mastercategory);
...will turn Men's into Menand#039;s

How can the string replace function do this when I am only asking it to concentrate on the &, and even so, why wouldn't the"$new_mastercategory = html_entity_decode($new_mastercategory);
...turn it into a more readable format?

Am I missing something?

This is starting to stretch my brain a bit now. It must be something SO simple.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Try doing the html_entity_decode before the str_replace. If you do an echo after each of those statements you will see why it is happening.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

It worked! it worked! IT WORKED! :)

Thank you so much, and you know, it makes so much sense now I look at it... bah! (kicking myself).

Thanks again :)


NOW:

$ShowMax = mysql_num_rows($content);
for ($y=1; $y<=$ShowMax; $y++)
{
$id = $Xcontent["id"];
$mastercategory = $Xcontent["mastercategory"];
$new_mastercategory = html_entity_decode($mastercategory);
$new_mastercategory = str_replace("&", "and", $new_mastercategory);
Post Reply