SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    PHP 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
    PHP Code:
    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
    PHP Code:
    $new    ereg_replace(' +'' '$new); 
    was removed, does it matter?

    Thank you

  2. #2
    SitePoint Enthusiast -RT-'s Avatar
    Join Date
    May 2014
    Location
    London, United Kingdom
    Posts
    42
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •