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

    Join Date
    Jun 2012
    Posts
    146
    Rep Power
    2

    Question Why doesn't this preg_replace work?


    Trying to replace any non digit value in a phone number text box.

    If I enter

    asd999asdf in the $phone text box and then press enter

    Code:
    echo $_POST['phone'];
    
    if ($_POST['phone'] != "") {
        if (preg_match('/[^0-9]/i', $_POST['phone'])) {
            echo "Non digit match found.";
            preg_replace('/[^0-9]/i', '', $_POST['phone']);
        }
    }
    
    echo $_POST['phone'];
    I get asd999asdf both before and after the preg_match, preg_replace. I know it's finding the non digits because it echo's out the statement.

    Ideas why the asd and asdf aren't stripped out with '' ?
  2. #2
  3. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    change it to this

    PHP Code:
    echo $_POST['phone'];

    if (
    $_POST['phone'] != "") {
        if (
    preg_match('/[^0-9]/i'$_POST['phone'])) {
            echo 
    "Non digit match found.";
            
    $_POST['phone'] = preg_replace('/[^0-9]/i'''$_POST['phone']); // notice what changed here
        
    }
    }

    echo 
    $_POST['phone']; 
    see the manual
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    146
    Rep Power
    2
    Wow, so simple. Thanks.

    I'm not sure I understand that though. Looking at the manual, examples 1 - 3 simply echo out the preg_replace without assigning it back to the original var which is essentially what I was doing. Why would those not work in my case, but example #4 (assigning it back to the original var) does?
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,296
    Rep Power
    7170
    examples 1 - 3 simply echo out the preg_replace without assigning it back to the original var which is essentially what I was doing. Why would those not work in my case, but example #4 (assigning it back to the original var) does?
    Your code doesn't echo the return value of preg_replace like the examples do; it echos the value of the original var. Therefore, in order to see the return value of preg_replace with your code, you have to assign the return value to the original var.

    Comments on this post

    • aeternus agrees
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    146
    Rep Power
    2
    Originally Posted by E-Oreo
    Your code doesn't echo the return value of preg_replace like the examples do; it echos the value of the original var. Therefore, in order to see the return value of preg_replace with your code, you have to assign the return value to the original var.
    I know that I was echoing the original var prior to the if statements.

    However, what I was wonder about was how could this:

    Mine:
    Code:
    preg_replace('/[^0-9]/i', '', $_POST['phone']);
    not, essentially be equal to this

    Manual:
    Code:
    echo preg_replace($pattern, $replacement, $string);
    In other words, the manual examples echo out the result of the replace without assigning that result to a new/different var. Isn't that basically saying that it's echoing out the (now) modified original var?
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,296
    Rep Power
    7170
    preg_replace does not modify the original var
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    146
    Rep Power
    2
    hmmm.

    since it's not modifying the original var, nor is it being assigned to a new var....what exactly is being echo'd that now doesn't resemble the original var (nor a new var that wasn't assigned)? Is it some kind of temp, or "working" variable that the system utilized just for the echo and then destroys?

    i guess it's not a big deal since it's now working, just currious as to why.

    thanks.
  14. #8
  15. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,296
    Rep Power
    7170
    The return value of the function is stored in the computer's main memory, but there are no user-space PHP variable identifiers that reference it. The value is not stored at a fixed location in memory; a new position is allocated every time preg_match is called.

    When PHP finishes evaluating the expression to the right of the echo token (ie: the preg_replace function call in this case), it will have the result of the whole expression stored in memory somewhere. It will then execute the echo token, passing it the memory location of the expression on the right, and echo will proceed to copy the result from memory directly into the output stream (which is elsewhere in the main memory).

    If, instead of echoing, you assign the value to a user-space PHP variable, then PHP will update the meta-data for the variable identifier that you used so that the variable references the location in memory where preg_match stored the final result. In this case, PHP will not free that piece of memory when the statement finishes execution.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    146
    Rep Power
    2
    Ok thanks. That makes more sense now.

IMN logo majestic logo threadwatch logo seochat tools logo