splitting lines in arrays?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tgiles

    splitting lines in arrays?

    Hi, all.

    Been staring at this for a couple of hours now and I find myself
    completely bewildered. I suppose it doesn't help that I'm a php newbie.
    Nevertheless, I throw myself at your mercy.

    I have an array which I am attempting to split off into a new array.
    The first array is just a whole bunch of links like so:




    I managed to (sorta) successfully extract things out using preg_split:

    foreach ($http_array as $i) {
    $split = preg_split("/=/", $i, 2);
    print "$split[1]<br>";
    }

    and the output looks like



    .... and so on...

    The main goal is to parse an array much like the first bit at the top,
    lop off anything after the first "=" in the line and then place it all
    into another blank array so that I might do something else clever
    elsewhere with it.

    however, i've been totally unsuccessful in cleaning out blank entries
    in the array (some creep in there, PREG_SPLIT_NO_E MPTY does not seem to
    catch them) and cramming all of that into another new array. Been back
    and forth between google and the fine php documents with little to show
    for it.

    eh. I give up. thoughts?

    tom

  • Janwillem Borleffs

    #2
    Re: splitting lines in arrays?

    tgiles wrote:[color=blue]
    > however, i've been totally unsuccessful in cleaning out blank entries
    > in the array (some creep in there, PREG_SPLIT_NO_E MPTY does not seem
    > to catch them) and cramming all of that into another new array. Been
    > back and forth between google and the fine php documents with little
    > to show for it.
    >[/color]

    PREG_SPLIT_NO_E MPTY doesn't work for you, because your error_reporting level
    is set too low.

    If you would prepend the following to your code, you will see a warning when
    the split didn't succeed:

    error_reporting (E_ALL);

    As you will see, $split[1] isn't always set and doing an isset($split[1]) or
    a count($split[1]) test before populating the target array helps to skip
    empty elements.

    As an alternative, you can also use preg_match to test for matches:

    foreach ($http_array as $i) {
    if (preg_match("/=(.+)/", $i, $split)) {
    print "$split[1]<br>";
    }
    }


    JW



    Comment

    • Senator Jay Billington Bulworth

      #3
      Re: splitting lines in arrays?

      "tgiles" <tgiles@gmail.c om> wrote in
      news:1113129126 .186261.205220@ g14g2000cwa.goo glegroups.com:
      [color=blue]
      > Hi, all.
      >
      > Been staring at this for a couple of hours now and I find myself
      > completely bewildered. I suppose it doesn't help that I'm a php
      > newbie. Nevertheless, I throw myself at your mercy.
      >
      > I have an array which I am attempting to split off into a new array.
      > The first array is just a whole bunch of links like so:
      >
      > http://www.example.com/query?track=h...se.com/whateve
      > r.php
      > http://www.example.com/query?track=h...se.com/whateve
      > r1.php
      >
      > I managed to (sorta) successfully extract things out using preg_split:
      >
      > foreach ($http_array as $i) {
      > $split = preg_split("/=/", $i, 2);
      > print "$split[1]<br>";
      > }
      >
      > and the output looks like
      >
      > http://www.somewhereelse.com/whatever.php
      > http://www.somewhereelse.com/whatever1.php
      > ... and so on...
      >
      > The main goal is to parse an array much like the first bit at the top,
      > lop off anything after the first "=" in the line and then place it all
      > into another blank array so that I might do something else clever
      > elsewhere with it.
      >
      > however, i've been totally unsuccessful in cleaning out blank entries
      > in the array (some creep in there, PREG_SPLIT_NO_E MPTY does not seem
      > to catch them) and cramming all of that into another new array. Been
      > back and forth between google and the fine php documents with little
      > to show for it.
      >
      > eh. I give up. thoughts?[/color]

      <?php

      //$http_array is already getting filled up somewhere else

      $urls_array = array();

      foreach($http_a rray as $i){
      #Break the line into two parts, saving the stuff after "track="
      list($junk, $url) = split('track=', trim(rtrim($i)) );
      #Make sure $url contains a value
      if(!empty($url) ){
      #See if $url is already in the $urls_array array
      if(!in_array($u rl, $urls_array)){
      #It's not, so let's add it
      array_push($url s_array, $url);
      }
      }
      }

      ?>

      Now you have a new array, $urls_array, which contains the URLs you want
      to work some magic on.

      hth


      --

      Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fu ng.arg');
      --------------------------|---------------------------------
      <http://www.phplabs.com/> | PHP scripts, webmaster resources

      Comment

      • Janwillem Borleffs

        #4
        Re: splitting lines in arrays?

        Senator Jay Billington Bulworth wrote:[color=blue]
        > #Break the line into two parts, saving the stuff after "track="
        > list($junk, $url) = split('track=', trim(rtrim($i)) );
        >[/color]

        Not a very good example, because:

        1. Using split instead of explode creates a lot of overhead
        2. You will get a warning when $i cannot be split into 2 elements and the
        error reporting level is set to E_ALL


        JW



        Comment

        • tgiles

          #5
          Re: splitting lines in arrays?


          Senator Jay Billington Bulworth wrote:[color=blue]
          > "tgiles" <tgiles@gmail.c om> wrote in
          > news:1113129126 .186261.205220@ g14g2000cwa.goo glegroups.com:
          >[color=green]
          > > Hi, all.
          > >
          > > Been staring at this for a couple of hours now and I find myself
          > > completely bewildered. I suppose it doesn't help that I'm a php
          > > newbie. Nevertheless, I throw myself at your mercy.
          > >
          > > I have an array which I am attempting to split off into a new[/color][/color]
          array.[color=blue][color=green]
          > > The first array is just a whole bunch of links like so:
          > >
          > >[/color][/color]
          http://www.example.com/query?track=h...se.com/whateve[color=blue][color=green]
          > > r.php
          > >[/color][/color]
          http://www.example.com/query?track=h...se.com/whateve[color=blue][color=green]
          > > r1.php
          > >
          > > I managed to (sorta) successfully extract things out using[/color][/color]
          preg_split:[color=blue][color=green]
          > >
          > > foreach ($http_array as $i) {
          > > $split = preg_split("/=/", $i, 2);
          > > print "$split[1]<br>";
          > > }
          > >
          > > and the output looks like
          > >
          > > http://www.somewhereelse.com/whatever.php
          > > http://www.somewhereelse.com/whatever1.php
          > > ... and so on...
          > >
          > > The main goal is to parse an array much like the first bit at the[/color][/color]
          top,[color=blue][color=green]
          > > lop off anything after the first "=" in the line and then place it[/color][/color]
          all[color=blue][color=green]
          > > into another blank array so that I might do something else clever
          > > elsewhere with it.
          > >
          > > however, i've been totally unsuccessful in cleaning out blank[/color][/color]
          entries[color=blue][color=green]
          > > in the array (some creep in there, PREG_SPLIT_NO_E MPTY does not[/color][/color]
          seem[color=blue][color=green]
          > > to catch them) and cramming all of that into another new array.[/color][/color]
          Been[color=blue][color=green]
          > > back and forth between google and the fine php documents with[/color][/color]
          little[color=blue][color=green]
          > > to show for it.
          > >
          > > eh. I give up. thoughts?[/color]
          >
          > <?php
          >
          > //$http_array is already getting filled up somewhere else
          >
          > $urls_array = array();
          >
          > foreach($http_a rray as $i){
          > #Break the line into two parts, saving the stuff after "track="
          > list($junk, $url) = split('track=', trim(rtrim($i)) );
          > #Make sure $url contains a value
          > if(!empty($url) ){
          > #See if $url is already in the $urls_array array
          > if(!in_array($u rl, $urls_array)){
          > #It's not, so let's add it
          > array_push($url s_array, $url);
          > }
          > }
          > }
          >
          > ?>
          >
          > Now you have a new array, $urls_array, which contains the URLs you[/color]
          want[color=blue]
          > to work some magic on.
          >
          > hth[/color]

          Cheers, Senator. Worked the first time. The comments were helpful as
          well in helping me figure out exactly what was going on.

          I appreciate the help

          tgiles

          Comment

          Working...