Jump to content

Stuck with preg_replace


Chappers

Recommended Posts

Hi,

 

I'm trying to use preg_replace to remove part of the IP address submitted using a form on my website. The full IP is entered into my SQL database, but when printing it on the website I want to remove some numbers, so turn for example: 82.132.196.45 into 82.132.***.** when printed on the page.

 

I find preg_replace very hard to use, though, and the PHP manual isn't very helpful for those who don't know what all the /wd<>\\n type things mean. I need to not only match numerals and replace each numeral with an asterisk, but get preg_replace to ignore the first two groups of numbers. I know this means telling the function to only pay attention to anything after the second dot, but don't know how.

 

Can anyone assist please?

Link to comment
Share on other sites

Shame it assumes that and doesn't even have a link for regular expressions. I don't see how it can explain a function with the assumption you already know about it, seems paradoxical.

 

Anyway, thank you but regular expressions still didn't help me with how to get the function to only replace integers after the second full-stop (dot). I believe it's known as using delimiters but Google just brings up thousand of pages of people doing things with delimiters exactly unlike I need to do, and often complex as hell. So I knocked this up which is adequate even if it replaces the actual number of integers after the dot with a string of 6 asterisks, and not the correct number.

 

<?php

$ip = '84.254.142.163';
$needle = '.';
$replacement = '***.***';

echo "IP before: $ip<br>";

$pos = stripos ($ip, $needle, $offset = 4);
$pos = $pos + 1;

$new_ip = substr_replace ($ip, $replacement, $pos);

echo "IP after: $new_ip";

?>

Link to comment
Share on other sites

I'm gonna be honest, I'm not much more than a novice at regex myself, this will work, but probably isn't the most simplistic way.

<?php
function parseip($ip)
{
$s = explode('.', $ip);
$s[2] = preg_replace('~(\d)~', '*', $s[2]);
$s[3] = preg_replace('~(\d)~', '*', $s[3]);
return implode('.', $s);
}
$ip = '82.132.196.45';
echo parseip($ip); //82.132.***.**

Link to comment
Share on other sites

Thanks for that, really appreciate your time and help.

 

I'm very novice at this, but don't have much time for learning deeply due to work hours, family, etc. My job is unrelated to computers and programming so I have to try and cram a bit of this into my brain when and if I can. I always try to do things myself because I get the feel good factor from that, but sometimes I don't have the time to learn enough to do it myself. And most of the time it's for basic websites I knock out for my own amusement or just to help spread information on subjects important to me. Hobby only, really. It's taken me till now (3am here!) just to sort out this IP problem and I have work in the morning! Oh well.

 

Thanks again.

Link to comment
Share on other sites

I wasn't thinking, this doesn't require anything too complicated. This is an easier solution:

<?php
function parseip($ip)
{
return preg_replace('~(\d+)\.(\d+)\.(\d+)\.(\d+)~', "$1.$2.*.*", $ip);
}
$ip = '82.132.196.45';
echo parseip($ip); //82.132.***.**

Link to comment
Share on other sites

Shame it assumes that and doesn't even have a link for regular expressions. I don't see how it can explain a function with the assumption you already know about it, seems paradoxical.

 

The preg_* function documentation only aims to document the functions themselves, not the patterns which can be provided to those functions.  For "those who don't know what all the /wd<>\\n type things mean" there is a series of pages describing the pattern syntax which details everything† you can use to construct a regular expression (though I'll admit, it's pretty dry reading!).

 

If the number of asterisks is important to you, and you really want to use a regular expression (rather than string functions, for example) then the snippet below will do what you're asking but it's not really a "basic" regular expression.

 

$ip = '12.34.123.4';
echo preg_replace('/(?:^(?:\d+\.){2}|)\K\d/', '*', $ip);
// 12.34.***.*

 

Another alternative would be to grab the last two octets (what you want to mask) and use a function to filter out the digits.

 

function mask_digits($m) { return preg_replace('/\d/', '*', $m[0]); }
echo preg_replace_callback('/\d+\.\d+$/', 'mask_digits', $ip);

 

† "everything" is not quite true, but it's more than enough for most purposes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.