ereg_replace is deprecated

Hi.

I was told the ereg_replace is deprecated, and I should use preg_replace, well I get lost easily with this stuff, and since I have an old form processing script that works fine, save the ereg_replace, will you please check, and in case correct the following:

old code

function clean_text_string($string) { // FORCE IT TO ACCEPTABLE ESCAPABLE CHARACTERS ONLY
        $new    = trim(ereg_replace('[^\\' a-zA-Z0-9&!#$%()"+:?/@,_\\.\\-]', '¿', stripslashes($string)));
        $new    = ereg_replace(' +', ' ', $new);
return ( $new );
}

new suggested one

function clean_text_string($string){ // FORCE IT TO ACCEPTABLE ESCAPABLE CHARACTERS ONLY
    $new = trim(preg_replace('~[^ a-zA-Z0-9&!#$%().,]+~', '¿', stripslashes($string)));
    return $new;
}

Reason I am asking is the kind person who suggested the change said is not an expert on preg_replace, also out of curiosity why the line

$new    = ereg_replace(' +', ' ', $new);

was removed, does it matter?

Thank you

Why don’t you write a unit test function? Then any time you change the clean_text_string() function, you can run your unit test function to make sure it works how you expect.