1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Using PHP to format numbers

Discussion in 'General PHP and MySQL Discussions' started by CodyBoen, Jan 12, 2014.

  1. CodyBoen

    CodyBoen Member

    Hi everyone,

    I am using the Custom Fields by Waindigo and I was just wondering if there is a way to format them value.

    I have a classifieds section and have a spot for price and milage. I would like 30000 to become 30,000 etc. I know how to do this in PHP with
    Code:
    $english_format_number = number_format($number);
    but I have no idea where to put it. Is there a <xen:> tag that I could use to achieve this or?

    Thanks for the help!!
     
  2. MattW

    MattW Well-Known Member

    xen:number

    eg:
    {xen:number $quikStats.views}
     
  3. CodyBoen

    CodyBoen Member

    Wow. Simple enough! Thank you so much. Been scratching my head over this for a while now.

    Is there a place where I can find all of the {xen:} helpers or whatever they're called??
     
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    Right now, you'll have to look into the code.
     
  5. Brogan

    Brogan XenForo Moderator Staff Member

    Look in library\XenForo\Template\Helper\Core.php, from line 95.
     
    MattW and Boothby like this.
  6. CodyBoen

    CodyBoen Member

    Is there any way to format phone numbers by any chance?

    Ex:
    1234567890 to (123) 456-7890 or 123-456-7890
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    This is a very basic example of how you could do it.

    PHP:
            $number 1234567890;
            if (
    strlen($number) === 10)
            {
                
    $part1 substr($number03);
                
    $part2 substr($number33);
                
    $part3 substr($number64);
                
    $formattedNumber "($part1$part2-$part3";

                
    Zend_Debug::dump($formattedNumber);
                
    // returns string(14) "(123) 456-7890"
            
    }
    You would perhaps want to make that much more robust by adding in some additional validation/error checking, but you get the idea.
     
    KiF likes this.
  8. CodyBoen

    CodyBoen Member

    So basically I would just add that to library\XenForo\Template\Helper\Core.php and than call it with xen:?
     
  9. Chris D

    Chris D XenForo Developer Staff Member

Share This Page