Technical articles
PHP and replacing Euro signs
Within PHP it can be bit of a pain when you want to replace euro signs that come from a database query.
This problem mostly has to do with different encodes of text. It’s very hard to covert an euro sign with a hard “€” within your replace code. I came across this problem a lot of times. This website covers the euro sign regarding different codes on different platforms.
I found using the chr function can be very successful. The following code has proven itself to be work on different websites to replace a euro signs within a String:
$string = str_replace(chr(128), ‘EUR’,$string);
Thanks for reading.
| Print article | This entry was posted by admin on June 4, 2010 at 9:41 pm, and is filed under PHP. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
And if you expect an UTF-8 encoded string (like from a web form which sends its data as UTF-8), you can use this one to replace the € with “EUR”:
$y = str_replace(chr(0xE2).chr(0×82).chr(0xAC),”EUR”,$x);
Cheers,
Lutz
about 1 year ago
That’s very usefull. Thanks for the info!
about 8 months ago
Thank you Lutz,
Its really works (chr(0xE2).chr(0×82).chr(0xAC) on my web form.
Thanks for posting.
Khalid
about 2 weeks ago
Gives me “unexpected T_STRING” error when using this.
about 2 weeks ago
You probably copy/pasted the code from your browser. The quotes might be corrupted. Try typing it yourself.
about 1 year ago
Big thx! I was stuck for hours on this “little” Problem and chr(128) finally worked.