View Full Version : regex replace
05-21-2008, 04:56 AM
I want to replace an ip:
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:
05-21-2008, 06:04 AM
you'd have to escape the literal . with \.
05-21-2008, 09:00 AM
You don't really need to load the regular expressions engine for this:
$parts = explode('.', $_SERVER['REMOTE_ADDR']);
echo $parts . '.***.***.' . $parts; // 127.***.***.1
However, for reference purposes, here is how you could do it with preg_replace() (http://php.net/preg_replace):
echo preg_replace('~(\d+)\.\d+\.\d+\.(\d+)~s', '\1.***.***.\2', $_SERVER['REMOTE_ADDR']); // 127.***.***.1
05-21-2008, 03:40 PM
ah, thank you very much. That answered my question.
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.