www.webdeveloper.com
Results 1 to 8 of 8

Thread: ereg or preg

  1. #1
    Join Date
    Jun 2005
    Posts
    30

    ereg or preg

    ereg or preg, whats the difference?

  2. #2
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    preg is faster and more powerfull
    you can also find this manual, btw
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    The ereg functions support the POSIX regular expression standard. The preg functions are based on the regex syntax and functions used in the Perl language. The preg functions support a few more options -- as to whether it's faster than ereg, I don't know about that. I use the preg functions mainly because I learned Perl before I learned PHP.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    @NogDog

    -- from http://www.php.net/ereg

    ereg

    (PHP 3, PHP 4, PHP 5)
    ereg -- Regular expression match

    Description
    int ereg ( string pattern, string string [, array &regs] )

    Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  5. #5
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    As pointed out above:
    • ereg == POSIX
    • preg == PCRE


    The most important difference (in my opinion) is that POSIX does not support lazy quantifiers.

  6. #6
    Join Date
    Jun 2005
    Posts
    30
    Quote Originally Posted by themarty
    preg is faster
    Is that true?
    Quote Originally Posted by bokeh
    The most important difference (in my opinion) is that POSIX does not support lazy quantifiers.
    What does that mean?

  7. #7
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    Is that true?
    just take a look at the quote in my second post.
    and if you don't believe that, you could always benchmark it yourself

    What does that mean?
    he's probably refering to the fact that posix doesn't support non-greedy matching
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  8. #8
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by themarty
    just take a look at the quote in my second post.
    and if you don't believe that, you could always benchmark it yourself
    Benchmarking proves that statment to be completely untrue by a factor of 10x (even more with long strings).

    One other difference is they match 2 different things. PCRE matches the first found left most match whereas POSIX matches the longest left most match.

    PHP Code:
    <?php

    $regex 
    'cat|category';
    $target 'category';

    echo 
    '<p>ereg: ' . (ereg($regex$target$match)? $match[0] : 'false').'</p>'// category

    echo '<p>preg: ' . (preg_match('/'.$regex.'/'$target$match)? $match[0] : 'false').'</p>'// cat

    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles