simple question : regular expressions

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

    simple question : regular expressions

    i have a string that looks something like this:
    -1--3--8--22--550--1028-

    how to make it an array like this:
    0 =1,
    1 =3,
    2 =8,
    3 =22,
    4 =550,
    5 =1028

    also, i would like to know some good reg exp tutorials, books or whatever that start from basics and that can teach me reg exp


    Thank you
  • amygdala

    #2
    Re: simple question : regular expressions


    "friglob" <friglob_MAKNI_ SVA_VELIKA_SLOV A_I_SVE_CRTICE_ @vip.hrschreef in
    bericht news:ebvgas$ekf $1@news1.xnet.h r...
    >i have a string that looks something like this:
    -1--3--8--22--550--1028-
    >
    how to make it an array like this:
    0 =1,
    1 =3,
    2 =8,
    3 =22,
    4 =550,
    5 =1028
    >
    also, i would like to know some good reg exp tutorials, books or whatever
    that start from basics and that can teach me reg exp
    >
    >
    Thank you
    <?php

    $string = '-1--3--8--22--550--1028-';
    $array = explode( '--', trim( $string, '-' ) );
    var_dump($array );

    ?>

    Will do the trick.

    Sorry, can't help you with the reg ex. But Google should provide lot's of
    info.


    Comment

    • friglob

      #3
      Re: simple question : regular expressions

      amygdala wrote:
      "friglob" <friglob_MAKNI_ SVA_VELIKA_SLOV A_I_SVE_CRTICE_ @vip.hrschreef in
      bericht news:ebvgas$ekf $1@news1.xnet.h r...
      >
      >>i have a string that looks something like this:
      >>-1--3--8--22--550--1028-
      >>
      >>how to make it an array like this:
      >>0 =1,
      >>1 =3,
      >>2 =8,
      >>3 =22,
      >>4 =550,
      >>5 =1028
      >>
      >>also, i would like to know some good reg exp tutorials, books or whatever
      >>that start from basics and that can teach me reg exp
      >>
      >>
      >>Thank you
      >
      >
      <?php
      >
      $string = '-1--3--8--22--550--1028-';
      $array = explode( '--', trim( $string, '-' ) );
      var_dump($array );
      >
      ?>
      >
      Will do the trick.
      >
      Sorry, can't help you with the reg ex. But Google should provide lot's of
      info.
      >
      >
      Just found out...

      preg_split("%-%",$string_to_s earch,-1,PREG_SPLIT_NO _EMPTY);

      Thank you for your time.

      Comment

      • Chung Leong

        #4
        Re: simple question : regular expressions

        friglob wrote:
        Just found out...
        >
        preg_split("%-%",$string_to_s earch,-1,PREG_SPLIT_NO _EMPTY);
        >
        Thank you for your time.
        preg_split("/-+/", $string_to_sear ch) is a somewhat more orthodox
        solution.

        Comment

        • wegenern@gmail.com

          #5
          Re: simple question : regular expressions

          $string = "-1--3--8--22--550--1028-";

          if(ereg
          ("-([0-9]{1})--([0-9]{1})--([0-9]{1})--([0-9]{2})--([0-9]{3})--([0-9]{4})-",
          $string, $regs)){
          $array_set = array_splice($r egs,1);
          print_r($regs);
          print_r($array_ set); // <- This is the output requested.
          }

          Chung Leong wrote:
          friglob wrote:
          Just found out...

          preg_split("%-%",$string_to_s earch,-1,PREG_SPLIT_NO _EMPTY);

          Thank you for your time.
          >
          preg_split("/-+/", $string_to_sear ch) is a somewhat more orthodox
          solution.

          Comment

          • Rik

            #6
            Re: simple question : regular expressions

            Don't toppost

            wegenern@gmail. com wrote:
            Chung Leong wrote:
            >friglob wrote:
            >>Just found out...
            >>>
            >>preg_split( "%-%",$string_to_s earch,-1,PREG_SPLIT_NO _EMPTY);
            >>>
            >>Thank you for your time.
            >>
            >preg_split("/-+/", $string_to_sear ch) is a somewhat more orthodox
            >solution.
            $string = "-1--3--8--22--550--1028-";
            >
            if(ereg
            >
            ("-([0-9]{1})--([0-9]{1})--([0-9]{1})--([0-9]{2})--([0-9]{3})--([0-9]{4})-",
            $string, $regs)){
            $array_set = array_splice($r egs,1);
            print_r($regs);
            print_r($array_ set); // <- This is the output requested.
            }
            But a lot more undynamic & slower, I'd suggest using Chung's solution. It's
            the fastest and the easiest altered.

            Grtz,
            --
            Rik Wasmus


            Comment

            Working...