#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Replacing ereg with preg_match


    Read the manual and searched the web still confused. Upgrading from php3 to php5. Afraid to touch this because I don't understand how it works. If anyone can help me upgrade to preg_match I would appreciate it.
    PHP Code:
    //ereg checks
    //username

    if(ereg('[^A-Za-z0-9]'$username)){
        
    $errors []= 'Username can only contain letters and numbers';
        
    $error true;
        
    $username_error true;
        }
    //password check
    if(ereg('[^A-Za-z0-9]'$password)){
        
    $errors []= 'Password can only contain letters and numbers';
        
    $error true;
        }
    //email check for valid syntax

    if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$"$email_address)) {
        
    $errors []= 'Please enter a valid Email address';
        
    $email_error true;
        
    $error true;

  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    In most cases you can replace ereg() with preg_match() like
    PHP Code:
    ereg('expression'$string)
    // becomes
    preg_match('/expression/'$string
    * Add /.../ delimiters to the expression

    and eregi() like
    PHP Code:
    ereg('expression'$string)
    // becomes
    preg_match('/expression/i'$string
    * Add /.../ delimiters to the expression
    * Use the /i flag for case-insensitivity
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0
    So
    PHP Code:
    if(ereg('[^A-Za-z0-9]'$username)){ 
    becomes
    PHP Code:
    if(preg_match('/[^A-Za-z0-9]/'$username)){ 
    and
    PHP Code:
    if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$"$email_address)) { 
    becomes
    PHP Code:
    if (!preg_match ("/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$/i"$email_address)) { 
    ?
    I think I get it from what I read. But why single quotes on one and double quotes on the other? Does it make a difference? Thanks, I'll try it. I'm just not sure about what I've read. It's just so ticky-tacky to me that I'm afraid to make a move.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    The difference is in PHP: characters in a string may be interpreted differently depending on which quoting style you use. Double-quoted strings allow variables and such inside and metacharacters like \n (newline) and \t (tab) are interpreted, while single-quoted strings don't allow variables and the only metacharacters that are interpreted are \\ and \'.

    PCRE (the preg_* family of functions) has its own metacharacters too. Most of the time you don't want variables inside the expression and any metacharacters you could put in are supported in PCRE too. I myself find it easier to just use single quotes all the time rather than concern myself with questions of whether something will be interpreted by PHP or not.

    I don't have any particularly good examples of the difference and when the quotes matter so meh.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    thanks


    thank you for taking your precious time to help me. I think I have enough to go ahead and make it work. I've been ignoring my code too long. my host is coddling me and not showing errors but I get them on my home server (wamp) so it's way past time to update. thanks again.

IMN logo majestic logo threadwatch logo seochat tools logo