Page 1 of 1
str_replace and & (amp;)
Posted: Sun Feb 27, 2005 7:45 am
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!

Posted: Sun Feb 27, 2005 8:03 am
by feyd
huh? you're str_replacing & with & .. and you wonder why & not appears in the new string? what are you smoking?
Posted: Sun Feb 27, 2005 8:06 am
by robster
sorry, that was a typo, the code is actually
$new_mastercategory = str_replace("&", "and", $mastercategory);
Posted: Sun Feb 27, 2005 8:34 am
by feyd
html_entity_decode()
Posted: Mon Feb 28, 2005 3:41 am
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++)
{
$id = $Xcontentї"id"];
$mastercategory = $Xcontentї"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

Posted: Tue Mar 01, 2005 6:09 am
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.
Posted: Tue Mar 01, 2005 6:32 am
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.
Posted: Tue Mar 01, 2005 6:37 am
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);