Re: Formating

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, July 4, 2005 6:48 pm, Bruno B B Magalhães said:
> For example I have a brazilian zipcode witch is stored in database as

Is she a Good Witch, or a Bad Witch? :-)

> 22252970 and must be formatted as NNNNN-NNN,  where N is a number.
> Also I have a tax id with is also stored as numeric value only, for
> example 05117635472 and outputted as NNN.NNN.NNN-NN... Is that any
> way that I can do it generic, storing the formatting strings ('NNNNN-
> NNN') with languages strings, so it is localised and this would be
> parsed as:
>
> string::format($string, $format);

//Untested code:
function format($string, $format){
  $slen = strlen($string);
  $flen = strlen($format);
  $result = '';
  for ($f = 0, $s = 0; $f <= $flen && $s <= $slen; $f++){
    $fc = $format[$f];
    $sc = $string[$s];
    switch($fc){
      case 'N':
        if (!strstr('0123456789', $sc)){
          //Suitable error for mal-formed data here.
          //$fc should be a digit, but it's not.
        }
        $result .= $sc;
        $s++;
      break;
      //Assume you need 'C'haracter data at some point in the future:
      case 'C':
        if (!stristr('abcdefghijklmnopqrstuvwxyz', $fc)){
          //more error-code (see above)
        }
        $result .= $sc;
        $s++;
      break;
      default:
        $result .= $fc;
      break;
    }
  }
  return $result;
}

I also don't think you want to tie it into "Locale" unless the data itself
is tagged with Locale, rather than the viewer's Locale.

A US zip code is NNNNN[-NNNN] no matter what language you are viewing it in.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux