[thelist] parsing numbers trap

Afonso Fernandez Nogueira afonso at writeme.com
Sat Jun 16 12:12:32 CDT 2001


I guess this octal issue only applies when you use regular expressions?

And speaking of regex, the php manual says the following:

"split ()
<snip>
Note that if you don't require the power of regular expressions, it is
faster to use explode(), which doesn't incur the overhead of the regular
expression engine. "

So, making this extensible to all the code, we are adviced to find
alternatives to regex where possible. Following this philosophy,  in this
case one would use the following to get the values for day, month and year:

$data = "2001-06-16";

$dd = substr($data, 8, 2);    // day
$mm = substr($data, 5, 2); // month
$yy = substr($data, 0, 4); // year
-- OR --
$d = explode("-", $data); // We get an array where $d[0] is the year, $d[1]
the month, $d[2] the day.

Are regex an important performance hit or can we placidly let ourselves fall
into regex addiction?

> > <tip type="mySQL, PHP">
> > just reading about Netscape's blooper, here is something to be wary
about:
> >
> > Given a Date field from mySQL like "2001-08-04" for August 4th 2001,
> > I used the
> >
> > function longDate($d) {
> > eval(ereg_replace('(#+)-(#+)-(#+)',
> > '$d = mktime(0,0,0,\\2,\\3,\\1);', $d));
> > return date ("l jS F Y", $d);
> > }
> >
> > to print something like "Saturday 4th August 2001",
> > but this does NOT work correct for Months or Days 08 and 09! Why?
> >
> > Numbers starting with zero are interpreted by PHP as octal (which go
from
> > 00 to 07). 08 and 09 are illegal octal numbers and taken for zero
> > which shows totally confused dates.
> >
> > The second line must read:
> > eval(ereg_replace('(#+)-0?(#+)-0?(#+)',
> > to eliminate the leading zeroes.
> >
> > </tip>






More information about the thelist mailing list