Jump to content

Recommended Posts

Hi there,

I'm trying to get the ips from a string using reg exp.

$file = "120.255.39.1 stringstring.zz.111.222.111.222 [150.100.100.2]";
$regexp = '/^((1?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(1?\d{1,2}|2[0-4]\d|25[0-5])$/';
preg_match_all($regexp, $file, $ips);
print_r($ips);

Obviously it's not working cause the string has to match $regexp for the array to get filled. I think I'm supposed to use preg_split and explode?! I'm sort of new to this, could anyone help please?

Can you list the rules for your match?  For something general you could use something like:

 

$file = "120.255.39.1 stringstring.zz.111.222.111.222 [150.100.100.2]";
$regexp = "~(\d{1,3}\.?){4}~";
preg_match_all($reg, $file, $ips);
print_r($ips[0]);

?>

 

This may match more then you desire, i.e. numbers over 255, which is why again, we need details.  You can take a gander at some examples here - http://www.regular-expressions.info/examples.html.

Well the ip validation was not that important, I managed to get what I wanted using preg_split. But I have another problem now..

 

let's say in.txt looks like this

text 222.333
235.232.124.122[][[][222.999.111.111]  text22 199.199.199.100 222.999.111.111

my code looks like this

<?php
$file = file_get_contents ('in.txt');
$sp = preg_split('/ /', preg_replace('/\[|\] /',' ',$file));
$regexp = '/^\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b$/';
$grep = preg_grep($regexp, $sp);
print_r($grep);
echo "<br /><br /><br />";
$j=0; $ip=NULL;
for ($i=0;$i<=sizeof($sp);$i++)
{	
if(isset($grep[$i])) 
{
        $ip[$j]=$grep[$i]; $j++;
}
}
print_r($ip);

Is there a function I can use to skip the elements in the array that repeat themselves? Like if I have an ip twice(like 222.999.111.111 in my example) or more times, can I just print it once, or even better, modify the array to just hold it once? .. this of course, without using another loop like a for or while

Works perfectly thanks, how about if I have another array $array2 and I want to exclude the elements in that array?

Like I don't want the ips in $array2 to exist(or at least be printed) in my array $ip that contains all the ips from in.txt

 

Is there a function for that? Or a more simple way to do it?

I think array_unique() had a limitation when comparing the arrays, had to print to file and read again so there'd be less elements in the array.

 

Anyway, now I can't figure out how to print the whole array to a text file with a new line between each ip.

 

obviously this doesn't work

<?php
$ip = implode("\n\r", $ip);
file_put_contents('out.txt', $ip);

should I use something different than implode?

<?php
$file = file_get_contents ('in.txt');

$npc[0]="1.1.1.1";
$npc[1]...
//and so on

$sp = preg_split('/ /', preg_replace('/\[|\] /',' ',$file));
$sp = array_unique($sp);


//$regexp = '/^\b(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b$/';
$regexp = '/^\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b$/';

$grep = preg_grep($regexp, $sp);
$sp = implode(' ', $sp);
file_put_contents('out.txt', $sp);
$file = file_get_contents ('out.txt');
$sp = preg_split('/ /', $file);
//could have made a function here I guess

$grep = preg_grep($regexp, $sp);
print_r($grep);
echo "<br /><br /><br />";
$j=0; $ip=NULL;
for ($i=0;$i<=sizeof($sp);$i++)
{	
if(isset($grep[$i])) 
{$ip[$j]=$grep[$i]; $j++;}
}
$ip = array_diff($ip,$npc);
print_r($ip);
$ip = implode(" ", $ip);
file_put_contents('out.txt', $ip);

Couldn't get it to work for notepad, guess I'll just leave it like that.

Thanks a lot for all the help.

This was a script for an online game ( www.slavehack.com ) to filter all the ips from the npc and slave ones, so I don't have to check every one, which takes a lot of time. Ofc I've done it to learn php, I'm not playing the game that much now.

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.