How to split a special string...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Reply-Via-Newsgroup

    How to split a special string...


    Folks,

    I have the following string:

    1P,2G,3G,3ML,4L ,5LB,4P

    Anybody know how I can easily seperate the letters and numbers in to
    multi-dimension array so as I have something like

    $myarray[0][0]="1";
    $myarray[0][1]="P";

    $myarray[1][0]="2";
    $myarray[1][1]="G";

    $myarray[2][0]="3";
    $myarray[2][1]="G";

    $myarray[3][0]="3";
    $myarray[3][1]="ML";

    $myarray[4][0]="4";
    $myarray[4][1]="L";

    $myarray[5][0]="5";
    $myarray[5][1]="LB";

    $myarray[6][0]="4";
    $myarray[6][1]="P";


    All help via newsgroup would be much appreciated...

    Thanks,
    Randell D.


  • Ian.H

    #2
    Re: How to split a special string...

    On Mon, 02 Feb 2004 23:12:25 +0000, Reply-Via-Newsgroup wrote:
    [color=blue]
    >
    > Folks,
    >
    > I have the following string:
    >
    > 1P,2G,3G,3ML,4L ,5LB,4P
    >
    > Anybody know how I can easily seperate the letters and numbers in to
    > multi-dimension array so as I have something like
    >
    > $myarray[0][0]="1";
    > $myarray[0][1]="P";
    >
    > $myarray[1][0]="2";
    > $myarray[1][1]="G";
    >[/color]


    [ snip ]


    [Untested...]

    $string = '1P,2G,3G,3ML,4 L,5LB,4P';
    $init_array = explode(',', $string);

    $i = 0;
    foreach ($init_array as $init_element) {
    preg_match(
    '#^([0-9]+)([a-zA-Z]+)$#',
    $init_element,
    $final_array[$i]
    );
    $i++
    }


    $final_array should be a as requested if I'm thinking straight =)
    [color=blue]
    > All help via newsgroup would be much appreciated...[/color]



    HTH.



    Regards,

    Ian

    --
    Ian.H
    digiServ Network
    London, UK


    Comment

    • Rahul Anand

      #3
      Re: How to split a special string...

      Try this code:
      [SNIP]

      function str_sep(&$val,$ key)
      {
      $val = preg_split('/(?<=\d)(?=\w)/', $val);
      }

      $str = "1P,2G,3G,3ML,4 L,5LB,4P";
      $myArray = explode(',',$st r);
      array_walk($myA rray,'str_sep') ;


      print_r($myArra y);


      [/SNIP]

      I guess it will do the required job.

      --
      Rahul Anand

      "Reply-Via-Newsgroup" <reply.via.news group.please@al l.times.com> wrote in message news:<tPATb.385 419$X%5.232967@ pd7tw2no>...[color=blue]
      > Folks,
      >
      > I have the following string:
      >
      > 1P,2G,3G,3ML,4L ,5LB,4P
      >
      > Anybody know how I can easily seperate the letters and numbers in to
      > multi-dimension array so as I have something like
      >
      > $myarray[0][0]="1";
      > $myarray[0][1]="P";
      >
      > $myarray[1][0]="2";
      > $myarray[1][1]="G";
      >
      > $myarray[2][0]="3";
      > $myarray[2][1]="G";
      >
      > $myarray[3][0]="3";
      > $myarray[3][1]="ML";
      >
      > $myarray[4][0]="4";
      > $myarray[4][1]="L";
      >
      > $myarray[5][0]="5";
      > $myarray[5][1]="LB";
      >
      > $myarray[6][0]="4";
      > $myarray[6][1]="P";
      >
      >
      > All help via newsgroup would be much appreciated...
      >
      > Thanks,
      > Randell D.[/color]

      Comment

      • Randell D.

        #4
        Re: How to split a special string...


        "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
        news:pan.2004.0 2.02.23.41.11.9 37935@hybris.di giserv.net...[color=blue]
        > On Mon, 02 Feb 2004 23:12:25 +0000, Reply-Via-Newsgroup wrote:
        >[color=green]
        > >
        > > Folks,
        > >
        > > I have the following string:
        > >
        > > 1P,2G,3G,3ML,4L ,5LB,4P
        > >
        > > Anybody know how I can easily seperate the letters and numbers in to
        > > multi-dimension array so as I have something like
        > >
        > > $myarray[0][0]="1";
        > > $myarray[0][1]="P";
        > >
        > > $myarray[1][0]="2";
        > > $myarray[1][1]="G";
        > >[/color]
        >
        >
        > [ snip ]
        >
        >
        > [Untested...]
        >
        > $string = '1P,2G,3G,3ML,4 L,5LB,4P';
        > $init_array = explode(',', $string);
        >
        > $i = 0;
        > foreach ($init_array as $init_element) {
        > preg_match(
        > '#^([0-9]+)([a-zA-Z]+)$#',
        > $init_element,
        > $final_array[$i]
        > );
        > $i++
        > }
        >
        >
        > $final_array should be a as requested if I'm thinking straight =)
        >[color=green]
        > > All help via newsgroup would be much appreciated...[/color]
        >
        >
        >
        > HTH.
        >
        >
        >
        > Regards,
        >
        > Ian
        >
        > --
        > Ian.H
        > digiServ Network
        > London, UK
        > http://digiserv.net/
        >[/color]

        Many thanks for that!


        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: How to split a special string...

          "Reply-Via-Newsgroup" <reply.via.news group.please@al l.times.com> wrote in message news:<tPATb.385 419$X%5.232967@ pd7tw2no>...[color=blue]
          > Folks,
          >
          > I have the following string:
          >
          > 1P,2G,3G,3ML,4L ,5LB,4P
          >
          > Anybody know how I can easily seperate the letters and numbers in to
          > multi-dimension array so as I have something like[/color]

          <snip>



          --
          "Success = 10% sweat + 90% tears"
          Email: rrjanbiah-at-Y!com

          Comment

          Working...