Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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 07:31 AM. Reason: adding more explaination

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    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.

  • Users who have thanked Phil Jackson for this post:

    wilcosky (09-18-2009)

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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 02:26 PM. Reason: adding text

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    wilcosky (09-18-2009)

  • #5
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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 04:43 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    wilcosky (09-18-2009)

  • #7
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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 06:51 PM.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    wilcosky (09-19-2009)

  • #9
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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!!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •