Can't replace £ with pound from string

I have an HTML string containing characters, for some reason I cannot replace them. I assume this is an encoding problem, although I cannot figure out how to do this. Site uses ISO-8859-1 to encode it.

$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 indicated that I want to replace £ with £ ; - I temporarily added £ to the array of elements to replace if it has already been converted

+7
source share
4 answers

Just to guess, but can it be that even your site comes out in ISO-8859-1 encoding, your actual * .php files are saved as utf-8? I don't think str_replace is working correctly using utf-8 strings. To check this, try:

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

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

+7
source
 html_entity_decode(str_replace("&pound;", "", htmlentities($str))); 
+1
source
 $str = '<span class="price">£89.99</span>'; echo str_replace("£","&pound;",$str); 

Output:

 <span class="price">&pound;89.99</span> 
0
source
 $data = str_replace("£","&pound;",utf8_encode($data)); echo $data; 
0
source

All Articles