Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-04-2011, 11:54 PM   PM User | #1
neutronical
New to the CF scene

 
Join Date: May 2011
Posts: 8
Thanks: 5
Thanked 0 Times in 0 Posts
neutronical is an unknown quantity at this point
Please help me with PHP problem!

Hi all,

Stuck on another problem. I have ip ranges in the format:

127.0.0.0 - 127.0.0.5 192.168.0.1 -192.168.0.5 192.168.0.6 - 192.168.0.9

So beginning number then 'space dash space' ending number space next beginning number...

I have a massive load of these as I am playing around with geo services.

But I cannot break them into an array. I'm guessing the easiest way is to use something like preg_split and set 'digit, space, digit' as the pattern (as the only times in the file the pattern 'digit, space, digit' appears is between two different ranges). I have no idea how to write these 'patterns' though, so if somebody could please help me out as I have been trying for hours now and its beginning to bug me!

Cheers In A
Dan
neutronical is offline   Reply With Quote
Old 07-05-2011, 12:51 AM   PM User | #2
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 892
Thanks: 4
Thanked 206 Times in 205 Posts
tracknut is an unknown quantity at this point
Are these all in one big long string? What jumps out at me is to use str_replace() to convert " - " (space dash space) to "-" (dash), and then explode() to select out pairs with the delimiter " " (space) into an array.

Dave
tracknut is offline   Reply With Quote
Users who have thanked tracknut for this post:
neutronical (07-06-2011)
Old 07-05-2011, 03:44 AM   PM User | #3
jimhill
Regular Coder

 
Join Date: Jul 2010
Posts: 271
Thanks: 3
Thanked 40 Times in 40 Posts
jimhill is an unknown quantity at this point
Your pattern isnt consistant. Try this

PHP Code:
<?php
$ax 
'127.0.0.0 - 127.0.0.5 192.168.0.1 -192.168.0.5 192.168.0.6 - 192.168.0.9';

$ax=str_replace(' -','-',$ax);
$ax=str_replace('- ','-',$ax);

$array explode(' ',$ax);
print_r($array);
?>
__________________
If you can't stand behind your troops, feel free to stand in front of them
Semper Fidelis
jimhill is offline   Reply With Quote
Users who have thanked jimhill for this post:
neutronical (07-06-2011)
Old 07-06-2011, 01:03 AM   PM User | #4
neutronical
New to the CF scene

 
Join Date: May 2011
Posts: 8
Thanks: 5
Thanked 0 Times in 0 Posts
neutronical is an unknown quantity at this point
Hi guys,
Just wanted to say that your advice paid off!
Works perfectly,
Cheers
neutronical is offline   Reply With Quote
Reply

Bookmarks

Tags
explode, pattern, perl, php, preg_split

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:25 AM.


Advertisement
Log in to turn off these ads.