• Pattern Matching using ereg() in PHP

    Definition and Usage

    The ereg() function searches a string specified by string for a string specified by pattern, returning true if the pattern is found, and false otherwise. The search is case sensitive in regard to alphabetical characters.

    The optional input parameter regs contains an array of all matched expressions that were grouped by parentheses in the regular expression.

    Syntax:

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

    Searches a string for matches to the regular expression given in pattern in a case-sensitive way.

    Parameter:

    Pattern: Case sensitive regular expression.

    String: Case sensitive regular expression.

    Regs: If matches are found for parenthesized substrings of pattern and the function is called with the third argument regs, the matches will be stored in the elements of the array regs.

    Return Value

    It returns true if the pattern is found, and false otherwise.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    <!--?php 
     
    $email_id = "admin@tutorialspoint.com";
     
    $retval = ereg("(\.)(com$)", $email_id);
     
    if( $retval == true )
     
    {
     
       echo "Found a .com<br ?-->";
     
    }
     
    else
     
    {
     
       echo "Could not found a .com
    ";
     
    }
     
    $retval = ereg(("(\.)(com$)"), $email_id, $regs);
     
    if( $retval == true )
     
    {
     
       echo "Found a .com and reg = ". $regs[0];
     
    }
     
    else
     
    {
      echo "Could not found a .com";
     
    }
     
    ?&gt;

Comments on this post

Leave a Reply

  • Security Code :


    two × = 10