Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: regex replace

  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    regex replace

    I want to replace an ip:
    69.102.832.225
    with
    69.***.***.225

    What would be the preg replace for that? I know that you can't use '.' in regex, so How do you do the whole like:

    (.*).(.*).(.*).(.*)

  • #2
    Regular Coder
    Join Date
    Sep 2006
    Location
    Vermont, USA
    Posts
    154
    Thanks
    0
    Thanked 6 Times in 6 Posts
    you'd have to escape the literal . with \.
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You don't really need to load the regular expressions engine for this:
    PHP Code:
    $parts explode('.'$_SERVER['REMOTE_ADDR']);
    echo 
    $parts[0] . '.***.***.' $parts[3]; // 127.***.***.1 
    However, for reference purposes, here is how you could do it with preg_replace():
    PHP Code:
    echo preg_replace('~(\d+)\.\d+\.\d+\.(\d+)~s''\1.***.***.\2'$_SERVER['REMOTE_ADDR']); // 127.***.***.1 

  • #4
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    ah, thank you very much. That answered my question.


  •  

    Posting Permissions

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