amelio Posted January 31, 2010 Share Posted January 31, 2010 Hi, I am having a problem when bringing in form input with the sterling character (£) prepended. I want to strip the character so I can then use the integer. $val == "£3.24" $val = str_replace ("£","",$val); This code returns "�3.24" and not "3.24". I don't understand why. I have looked around and people suggest the need to use... <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> I have run the script with and without to no avail. Very grateful of any help Quote Link to comment https://forums.phpfreaks.com/topic/190442-str_replace-returning-fffd-character/ Share on other sites More sharing options...
MasterACE14 Posted January 31, 2010 Share Posted January 31, 2010 I'm not positive, but you might have to use the ascii for it instead... $val = "£3.24"; $val = str_replace ("£","",$val); and if you just want to remove the symbol from the front of your string, just use substr function instead... http://au.php.net/manual/en/function.substr.php Quote Link to comment https://forums.phpfreaks.com/topic/190442-str_replace-returning-fffd-character/#findComment-1004583 Share on other sites More sharing options...
amelio Posted January 31, 2010 Author Share Posted January 31, 2010 Thanks for that. I tried substr() and couldn't get that to work. I should have explained myself better. Sometimes I do have just numeric values coming into the form and other times with the "£" prepended. Also I did try to use "£" but then it wasn't finding the match to replace with empty quotes. I was getting "£3.24" in to str_replace and the same out. I just don't get where the FFFD character is coming from. If I knew what character FFFD represented I guess I could run a second str_replace to replace that but when I paste that character into the code I just get a small one character size box. Quote Link to comment https://forums.phpfreaks.com/topic/190442-str_replace-returning-fffd-character/#findComment-1004596 Share on other sites More sharing options...
premiso Posted January 31, 2010 Share Posted January 31, 2010 $val == "£3.24" $val = str_replace (chr(ord("£")),"",$val); Using the char with ord maybe that will help this script locate the actual character. Give that a shot see if it works for ya. Quote Link to comment https://forums.phpfreaks.com/topic/190442-str_replace-returning-fffd-character/#findComment-1004613 Share on other sites More sharing options...
amelio Posted January 31, 2010 Author Share Posted January 31, 2010 Thanks for that Premiso but to no avail. I even tried this if (strpos($val,"£") == 0) { $val = str_replace ("£","",$val); $val = (float) $val; }; I thought if I explicitly cast the input to a float then that might help rid me of the darn character but still no. Tearing my hair out on this one. Appreciate your help though. Quote Link to comment https://forums.phpfreaks.com/topic/190442-str_replace-returning-fffd-character/#findComment-1004705 Share on other sites More sharing options...
premiso Posted January 31, 2010 Share Posted January 31, 2010 I just tried this: <?php $val = " £23443"; if (strpos($val,"£") !== false) { $val = str_replace ("£","",$val); echo $val; } on my server and it worked just fine... Quote Link to comment https://forums.phpfreaks.com/topic/190442-str_replace-returning-fffd-character/#findComment-1004735 Share on other sites More sharing options...
amelio Posted February 1, 2010 Author Share Posted February 1, 2010 I think the problem boils down to the javascript sliding tab that I have. I'm no expert at javascript and wanted a simple tab script so looked around and found one. except it's a bit more complicated than I originally wanted but has a cool sliding effect. I plugged my php into that and I have this problem. When i revert back to the older design, like you, the code works no problem. I still can't understand why the javascript would cause this. It's so weird I tested a form input of "£3.24" and it told me it was a string with 6 characters. This seemed very strange as it only had 5. So, I displayed the value of each of the string elements and the first two of the six was that FFFD character, so not only was that character replacing the sterling "£" character I also had a phantom character that was not displaying but was being captured by var_dump. I think I will ditch the cool javascript effect, it costs me too much hair! Appreciate the help given and if anyone knows what the heck is going on here I would be fascinated because I would love to know why I've lost a sunday I won't get back! Quote Link to comment https://forums.phpfreaks.com/topic/190442-str_replace-returning-fffd-character/#findComment-1004832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.