...

View Full Version : Allow currency symbols for cost field



wilcosky
09-18-2009, 04:53 AM
Hello,

How can I edit the following line to not only allow $, but also € and ? I added [$] at the beginning which works for the dollar sign, but if I try adding [€], for example, it doesn't work. Any thoughts? Maybe it has something to do with ASC II encoding vs UTF-8. Because when I type € into my code and save it, it automatically changes it to the html value €.


if (!ereg("^([$][0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$", $cost))

This isn't crucial if I can't figure it out, but it would be nice.

Phil Jackson
09-18-2009, 08:36 AM
$xmp = "<xmp>".$cost."</xmp>";
die($xmp);
if (!ereg("^([$][0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$", $cost))

stick that in for me and tell me what it says.

wilcosky
09-18-2009, 01:51 PM
$xmp = "<xmp>".$cost."</xmp>";
die($xmp);
if (!ereg("^([$][0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$", $cost))

stick that in for me and tell me what it says.

If I stick that in and submit the form with €4.00, I simply get €4.00 on the page that normally gives me errors or tells me the form was submitted. Which might be a good sign? At least my form can properly submit the € sign. It just can't validate it.

So from this test, I've come to the conclusion that the reason it won't work is because the php file that the above code is in, is encoded using US ASC II. I think to get it to work I have to convert the code to UTF-8. My site can read UTF-8, which is why the above test worked, however, the file with the cost function in it, can't read UTF-8.

Honestly though, I'm afraid to convert the file from US ASC II to UTF-8 because isn't there a chance that something else in the code could get messed up?

So, my final question is: Is there a way to get this to work without changing the encoding of my php file?

Fou-Lu
09-18-2009, 02:48 PM
What you see doesn't ever reach you're ereg. Change that to a preg btw.
UTF-8 is not natively supported prior to php6, and since that hasn't been released yet I would not recommend converting.
Instead, we can use the ordinal value of the character instead. I can't test ATM, but I believe the euro is 0x80. So, when using pcre, you can pattern match hex characters with: \x80.
Test that with:


if (!preg_match('/^\x80$/', $input))
{
print 'failed';
}

Where input is you're euro symbol, submitted to the page.

wilcosky
09-18-2009, 04:40 PM
Thanks for the help guys! I think I'm also going to try just allowing all characters. :rolleyes: Why? Because I've decided that I really don't care what people put into the cost field. Hopefully they'll be nice and put an actual cost, but if they don't it's not a big deal.

I already have a 7 character limit on this cost field anyways. So, if I allow all characters, then people can put $5.00 or "free" or "none" or €5 or whatever they want as long as it isn't more than 7 characters.

This is taking the easy way out I know, but, I'm cool with that. :thumbsup:

Fou-Lu
09-18-2009, 05:06 PM
Don't give it up, this is what you'll need (I confirmed that 80 and A3 are the euro and pound symbols): [\$|\x80|\xA3]?. Use that in place of you're [$] sign, and use a preg_match instead of ereg instead (the rest of the code is the same syntax, so it will work).
That will allow you to use a $, euro or pound leader, but it isn't required. Too bad it has to be a leader character, if you wanted to add suffix symbols, this becomes more complicated.

wilcosky
09-18-2009, 06:38 PM
Don't give it up, this is what you'll need (I confirmed that 80 and A3 are the euro and pound symbols): [\$|\x80|\xA3]?. Use that in place of you're [$] sign, and use a preg_match instead of ereg instead (the rest of the code is the same syntax, so it will work).
That will allow you to use a $, euro or pound leader, but it isn't required. Too bad it has to be a leader character, if you wanted to add suffix symbols, this becomes more complicated.


Well, that looks really easy so I'll try that tonight when I get home. Thank you very much!!

It is a shame that the symbols must be a leader character, but, at this point I'm not being picky.;)

To confirm, is this line correct?


if (!preg_match("^[\$|\x80|\xA3]?[0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$", $cost))

Is the question mark needed after the xA3]?

Fou-Lu
09-19-2009, 04:22 AM
The question mark indicates 0 or 1. So the input may or may not have a dollar, euro or pound symbol.
You have one too many closing brackets, likely here: (,[0-9]{3})*). Remove the last one. That should give you a match for what you're looking for.

wilcosky
09-19-2009, 05:53 AM
The question mark indicates 0 or 1. So the input may or may not have a dollar, euro or pound symbol.
You have one too many closing brackets, likely here: (,[0-9]{3})*). Remove the last one. That should give you a match for what you're looking for.


worked! thanks again!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum