Need Help Fixing "Deprecated" Function

Discussion in 'PHP' started by gregdavidson, May 1, 2014.

  1. #1
    Hi,

    I just installed a new script on my website and I'm getting this message:

    Deprecated: Function ereg_replace() is deprecated in /home/content/68/9317268/html/YourSite/seourls.php on line 23

    This is the coding that's on line 23:

    $return = trim(ereg_replace(' +',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($input))));

    If you can help me I would greatly appreciate it. Thanks.
     
    gregdavidson, May 1, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Just remove it.
    $return = trim(preg_replace('/[a-z0-9\s]/','',strtolower($input)));
    does the same thing, as far as I can see.
     
    PoPSiCLe, May 1, 2014 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Sorry @PoPSiCLe, not the same thing. The e-reg is there to condense multiple spaces to a single space... See that space before the plus?

    Normally I'd assume that's trying to do FULL whitespace compression, in which case I'd go with:
    $return = trim(preg_replace('/\s+/',' ',preg_replace('/[^a-z0-9\s]/','',strtolower($input))));

    * NOTE - I killed off the upper-case check in the regex since it already runs strtolower.

    Though if you literally just want any number of spaces ONLY, just add the slashes...

    $return = trim(preg_replace('/ +/',' ',preg_replace('/[^a-z0-9\s]/','',strtolower($input))));

    Though that could be even more simply done since patterns are type mixed -- so you can pass arrays.

    $return = trim(preg_replace(['/\s+/', '/[^a-z0-9\s]/'], [' ', ''], strtolower($input)));

    I'd go with the \s so ALL whitespace chars are turned into spaces.

    * NOTE - uses PHP 5.4 style arrays.

    Though that is REALLY complex and semi-questionable. I'd be wondering about the data that's being scrubbed by that.
     
    deathshadow, May 1, 2014 IP