jokerbla Posted May 28, 2009 Share Posted May 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/ Share on other sites More sharing options...
Maq Posted May 29, 2009 Share Posted May 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-844622 Share on other sites More sharing options...
jokerbla Posted June 4, 2009 Author Share Posted June 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849325 Share on other sites More sharing options...
Maq Posted June 4, 2009 Share Posted June 4, 2009 Try - array_unique. Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849328 Share on other sites More sharing options...
jokerbla Posted June 4, 2009 Author Share Posted June 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849334 Share on other sites More sharing options...
Maq Posted June 4, 2009 Share Posted June 4, 2009 If you put them both into arrays you can use - array_diff. Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849340 Share on other sites More sharing options...
jokerbla Posted June 4, 2009 Author Share Posted June 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849382 Share on other sites More sharing options...
Maq Posted June 4, 2009 Share Posted June 4, 2009 That should work, can I see the current code? Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849388 Share on other sites More sharing options...
jokerbla Posted June 4, 2009 Author Share Posted June 4, 2009 <?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); Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849391 Share on other sites More sharing options...
Maq Posted June 4, 2009 Share Posted June 4, 2009 The print_r($ip); prints out an array right? Try changing these last lines. $input = implode("\n", $ip); file_put_contents("out.txt", $input); Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849394 Share on other sites More sharing options...
jokerbla Posted June 4, 2009 Author Share Posted June 4, 2009 still doesn't work with notepad in wordpad it does the new lines what's the code for new line in the encoding notepad uses? (perhaps that might work?) Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849397 Share on other sites More sharing options...
jokerbla Posted June 4, 2009 Author Share Posted June 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160079-solved-php-retrieve-ip-from-string-using-reg-exp/#findComment-849416 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.