SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ereg vs preg_match

    Ok, I am confused. I do this:
    Code:
    echo preg_match("[^A-Za-z0-9@\._-]","David@;");
    echo ereg("[^A-Za-z0-9@\._-]","David@;");
    preg_match outputs a 0.
    ereg outputs a 1.

    Shouldn't they both detect the semi colon and output a 1?
    --David Reagan
    DavidReagan.net

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The ereg functions are slow and old. Forget about them and use preg.

    I think preg is failing because you need to delimit the regular expression with a pair of characters, "/" is often used:

    PHP Code:
    preg_match("/[^A-Za-z0-9@\._-]/","David@;"); 
    If you just want to establish whether there's a semicolon present, forget about using regular expressions and just use string matching functions, e.g. strpos.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    The ereg functions are slow and old. Forget about them and use preg.

    I think preg is failing because you need to delimit the regular expression with a pair of characters, "/" is often used:

    PHP Code:
    preg_match("/[^A-Za-z0-9@\._-]/","David@;"); 
    If you just want to establish whether there's a semicolon present, forget about using regular expressions and just use string matching functions, e.g. strpos.
    Ah, the slashes worked... Um, why the delimiters? I didn't see anything about them on regular-expressions.info or php.net/preg_match.

    Yeah, I was trying to use the preg functions because I read that they are better in the tips topic on here, but they weren't working.... Now that I know about the delimiting though, I should be ok.

    Thanks!
    --David Reagan
    DavidReagan.net

  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jerrac View Post
    Ah, the slashes worked... Um, why the delimiters? I didn't see anything about them on regular-expressions.info or php.net/preg_match.

    Yeah, I was trying to use the preg functions because I read that they are better in the tips topic on here, but they weren't working.... Now that I know about the delimiting though, I should be ok.

    Thanks!
    http://www.regular-expressions.info/php.html

    All of the preg functions require you to specify the regular expression as a string using Perl syntax. In Perl, /regex/ defines a regular expression. In PHP, this becomes preg_match('/regex/', $subject). Forward slashes in the regular expression have to be escaped with a backslash. So http://www\.jgsoft\.com/ becomes '/http:\/\/www\.jgsoft\.com\//'. Just like Perl, the preg functions allow any non-alphanumeric character as regex delimiters. The URL regex would be more readable as '%http://www\.jgsoft\.com/%' using percentage signs as the regex delimiters.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I hadn't found that page yet.
    --David Reagan
    DavidReagan.net


Bookmarks

Posting Permissions

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