...

View Full Version : regex replace



thesavior
05-21-2008, 04:56 AM
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:

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

syosoft
05-21-2008, 06:04 AM
you'd have to escape the literal . with \.

Inigoesdr
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[0] . '.***.***.' . $parts[3]; // 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

thesavior
05-21-2008, 03:40 PM
ah, thank you very much. That answered my question.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum