PHP: preg_match and ereg

  • cancer10
  • Proficient
  • Proficient
  • cancer10
  • Posts: 268

Post 3+ Months Ago

Hi,

I tried the following two functions to check if a variable is numeric or not. The first function gave me a negative result.

PHP Code: [ Select ]
<?php
 
$myvar = 123;
if(!ereg("^[\d]*\.?\d*$",$myvar))
echo "Amount Should be Numeric.";
else
echo "Amount is numeric";
?>
  1. <?php
  2.  
  3. $myvar = 123;
  4. if(!ereg("^[\d]*\.?\d*$",$myvar))
  5. echo "Amount Should be Numeric.";
  6. else
  7. echo "Amount is numeric";
  8. ?>


But the second function worked.
PHP Code: [ Select ]
<?php
$myvar = 123;
if(!preg_match("/^[\d]*\.?\d*$/",$myvar))
echo "Amount Should be Numeric.";
else
echo "Amount is numeric";
?>
  1. <?php
  2. $myvar = 123;
  3. if(!preg_match("/^[\d]*\.?\d*$/",$myvar))
  4. echo "Amount Should be Numeric.";
  5. else
  6. echo "Amount is numeric";
  7. ?>



I could not understand the mystery behind it.

Can you explain please?


Thanx
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

PCRE = Good.
EREG = Bad.

If you know PCRE, don't bother with the ereg functions at all. No sense in learning the quirks of one if you aren't going to use it.

FTR, your code would be more readable and conventional if you avoided using the not operator in an if statement like you have. You read, "if true then ____ else ____". It gets confusing when your conditional is negated so you're reading, "if ___ not true then ____ else ____".

PHP Code: [ Select ]
<?
 
if (preg_match("...", $foo))
  // true
else
  // false
 
?>
  1. <?
  2.  
  3. if (preg_match("...", $foo))
  4.   // true
  5. else
  6.   // false
  7.  
  8. ?>
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I have to agree with Truce, mainly because of an important note on any of the ereg php.net manual pages.

Quote:
Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().


I also have to agree about not using the not operator in that situation, though I wouldn't even be using the else most likely.
I would either have a default value set for any variables before that IF statement and set them to alternatives inside the IF, or I would be checking whether certain variables were set insde that IF using isset in later code.

Code: [ Select ]
$result = 'default';

if(preg_match(..., $matches))
{
$result = work_with($matches);
}

handle($result);
  1. $result = 'default';
  2. if(preg_match(..., $matches))
  3. {
  4. $result = work_with($matches);
  5. }
  6. handle($result);


Code: [ Select ]
if(preg_match(..., $matches))
{
$result = work_with($matches);
}

handle(isset($result) ? $result : 'default');
  1. if(preg_match(..., $matches))
  2. {
  3. $result = work_with($matches);
  4. }
  5. handle(isset($result) ? $result : 'default');
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

And to answer your orignal question, it looks like you're using PCRE syntax in both patterns, whch may be why the ereg result differs form the preg result.
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

For the record, php has several built in functions that check if a variable is numeric:
is_numeric() Checks if a var is numeric (1234, or +1234.56 etc)
is_int() Checks if a var is an integer
is_float() Checks if a var is a float
and several others.
They tend to run much faster than regular expressions, so unless you absolutely need REs, it's usually better to use built in functions.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 118 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.