Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot replace £ with &pound from string

I have a HTML string containing £ signs, for some reason i'm not able to replace them. I'm assuming this is an encoding issue although i can't work out how. The site is using ISO-8859-1 for its encoding

$str = '<span class="price">£89.99</span>';
var_dump(mb_detect_encoding($str, 'ISO-8859-1', true)); // outputs ISO-8859-1

echo str_replace(array("£","&pound;"),"",$str); // nothing is removed

echo htmlentities($str); // the entire string is converted, including £ to &pound;

Any ideas?

EDIT

should have pointed out i want to replace the £ with &pound; - i had temporarily added &pound to the array of items to replace in case it had already been converted

like image 939
robjmills Avatar asked Nov 22 '11 13:11

robjmills


2 Answers

Just a guess but could it be that even thou your website outputs in ISO-8859-1 encoding, your actual *.php files are saved as utf-8? i don't think that str_replace works correctly with utf-8 strings. To test it try:

str_replace(utf8_decode("£"),"&pound;",utf8_decode($str));

Yeah, if this works then your *.php files are saved in utf-8 encoding. This means all the string constants are in utf-8. It's probably worth switching default encoding in your IDE to ISO-8859-1

like image 149
Ivan Avatar answered Sep 18 '22 14:09

Ivan


html_entity_decode(str_replace("&pound;", "", htmlentities($str)));
like image 40
Jan Vorcak Avatar answered Sep 20 '22 14:09

Jan Vorcak