Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,348
    Thanks
    11
    Thanked 289 Times in 288 Posts
    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

  • Users who have thanked tracknut for this post:

    neutronical (07-06-2011)

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Posts
    271
    Thanks
    3
    Thanked 40 Times in 40 Posts
    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

  • Users who have thanked jimhill for this post:

    neutronical (07-06-2011)

  • #4
    New to the CF scene
    Join Date
    May 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi guys,
    Just wanted to say that your advice paid off!
    Works perfectly,
    Cheers


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •