Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-18-2009, 03:53 AM   PM User | #1
wilcosky
New Coder

 
Join Date: Mar 2008
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
wilcosky has a little shameless behaviour in the past
Allow currency symbols for cost field

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 €.

PHP Code:
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.

Last edited by wilcosky; 09-18-2009 at 06:31 AM.. Reason: adding more explaination
wilcosky is offline   Reply With Quote
Old 09-18-2009, 07:36 AM   PM User | #2
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
PHP Code:
$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.
Phil Jackson is offline   Reply With Quote
Users who have thanked Phil Jackson for this post:
wilcosky (09-18-2009)
Old 09-18-2009, 12:51 PM   PM User | #3
wilcosky
New Coder

 
Join Date: Mar 2008
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
wilcosky has a little shameless behaviour in the past
Quote:
Originally Posted by Phil Jackson View Post
PHP Code:
$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?

Last edited by wilcosky; 09-18-2009 at 01:26 PM.. Reason: adding text
wilcosky is offline   Reply With Quote
Old 09-18-2009, 01:48 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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:
PHP Code:
if (!preg_match('/^\x80$/'$input))
{
    print 
'failed';

Where input is you're euro symbol, submitted to the page.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
wilcosky (09-18-2009)
Old 09-18-2009, 03:40 PM   PM User | #5
wilcosky
New Coder

 
Join Date: Mar 2008
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
wilcosky has a little shameless behaviour in the past
Thanks for the help guys! I think I'm also going to try just allowing all characters. 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.

Last edited by wilcosky; 09-18-2009 at 03:43 PM..
wilcosky is offline   Reply With Quote
Old 09-18-2009, 04:06 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
wilcosky (09-18-2009)
Old 09-18-2009, 05:38 PM   PM User | #7
wilcosky
New Coder

 
Join Date: Mar 2008
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
wilcosky has a little shameless behaviour in the past
Quote:
Originally Posted by Fou-Lu View Post
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?

PHP Code:
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]?

Last edited by wilcosky; 09-18-2009 at 05:51 PM..
wilcosky is offline   Reply With Quote
Old 09-19-2009, 03:22 AM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
wilcosky (09-19-2009)
Old 09-19-2009, 04:53 AM   PM User | #9
wilcosky
New Coder

 
Join Date: Mar 2008
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
wilcosky has a little shameless behaviour in the past
Quote:
Originally Posted by Fou-Lu View Post
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!!
wilcosky is offline   Reply With Quote
Reply

Bookmarks

Tags
currency, ereg, php, regex

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:59 PM.


Advertisement
Log in to turn off these ads.