Jump to content

str_replace returning FFFD character


amelio

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/190442-str_replace-returning-fffd-character/
Share on other sites

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

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 "&#163;" 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.

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.

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.