Need help to resolve code problem in general.php

Discussion in 'PHP' started by Functional, Jan 13, 2012.

  1. #1
    Can't seem to get this working. Need help to resolve code problem in catalog/includes/functions/general.php

    To work in new PHP 5.3 former code "ereg" needs to be changed to "preg-match".

    Changed 2 of the three lines as below:

    Here is the code:

    if ($type == 'mixed') {
    if (preg_match('^[a-z0-9]$', $char)) $rand_value .= $char;
    } elseif ($type == 'chars') {
    if (preg_match('^[a-z]$', $char)) $rand_value .= $char;
    } elseif ($type == 'digits') {
    if (eregi('^[0-9]$', $char)) $rand_value .= $char;
    }
    }

    But when I change the 3rd one which is "eregi" with the extra "i" to "preg_match" which is suppose to be changed to the same change as "ereg"
    I get this error:

    Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /includes/functions/general.php on line 1043

    Appreciate any help to resolve this.
     
    Functional, Jan 13, 2012 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Something like this should work. Looks like you just need the forward slash inside the matching criteria.

    if (preg_match('/^[a-z0-9]$/', $char)) $rand_value .= $char;
    } elseif ($type == 'chars') {
    if (preg_match('/^[a-z]$/', $char)) $rand_value .= $char;
    } elseif ($type == 'digits') {
    if (preg_match('/^[0-9]$/', $char)) $rand_value .= $char;
     
    jestep, Jan 13, 2012 IP
  3. Functional

    Functional Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you so much, that worked.
    I do have a new error I'm stuck on:

    Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0 in /includes/functions/general.php on line 61

    Here is the code:

    function tep_sanitize_string($string) {
    $string = preg_replace(' /+/', ' ', trim($string));

    return preg_replace("/[<>]/", '_', $string);
    }

    Line 61 is in Bold.
     
    Functional, Jan 13, 2012 IP
  4. modz

    modz Well-Known Member

    Messages:
    95
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    103
    #4
    I think you should change the regular expression to '/( +)/'.
     
    modz, Jan 15, 2012 IP