preg_match digits?

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

    preg_match digits?

    What is the most efficient way of extracting the first two digits in a
    string?

    The following is wrong for me, because it only gives me the first
    instance of two digits together:

    $string = ujdk3ca94abc
    preg_match("/\d{2}/",$string,$resu lt);
    echo "$result[0]";
    //prints 94. However, the result I am looking for is 39

    Here's another example:
    $string = 'abc 8a bc abc934';
    //desired result = 89

    Thank you.



  • Tim Van Wassenhove

    #2
    Re: preg_match digits?

    In article <40BBDBB4.F0F19 AE8@nospamun8no spam.com>, Westcoast Sheri wrote:[color=blue]
    > What is the most efficient way of extracting the first two digits in a
    > string?
    >
    > The following is wrong for me, because it only gives me the first
    > instance of two digits together:[/color]

    $found = '';

    $index = 0;

    while (strlen($found) < $needed && $index < strlen($string) ) {
    if (is_numeric($st ring{$index})) {
    $found .= $string{$index} ;
    }
    ++$index;
    }


    --
    Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>

    Comment

    • Westcoast Sheri

      #3
      Re: preg_match digits?

      Tim Van Wassenhove wrote:
      [color=blue]
      > In article <40BBDBB4.F0F19 AE8@nospamun8no spam.com>, Westcoast Sheri wrote:[color=green]
      > > What is the most efficient way of extracting the first two digits in a
      > > string?
      > >
      > > The following is wrong for me, because it only gives me the first
      > > instance of two digits together:[/color]
      >
      > $found = '';
      >
      > $index = 0;
      >
      > while (strlen($found) < $needed && $index < strlen($string) ) {
      > if (is_numeric($st ring{$index})) {
      > $found .= $string{$index} ;
      > }
      > ++$index;
      > }
      >
      > --
      > Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>[/color]

      Tim, that's a joke, right?




      Comment

      • Tim Van Wassenhove

        #4
        Re: preg_match digits?

        In article <40BBED21.44389 456@nospamun8no spam.com>, Westcoast Sheri wrote:[color=blue]
        > Tim Van Wassenhove wrote:
        >[color=green]
        >> In article <40BBDBB4.F0F19 AE8@nospamun8no spam.com>, Westcoast Sheri wrote:[color=darkred]
        >> > What is the most efficient way of extracting the first two digits in a
        >> > string?
        >> >
        >> > The following is wrong for me, because it only gives me the first
        >> > instance of two digits together:[/color]
        >>
        >> $found = '';
        >>
        >> $index = 0;
        >>
        >> while (strlen($found) < $needed && $index < strlen($string) ) {
        >> if (is_numeric($st ring{$index})) {
        >> $found .= $string{$index} ;
        >> }
        >> ++$index;
        >> }[/color][/color]
        [color=blue]
        > Tim, that's a joke, right?[/color]

        1-) It works.
        2-) I presume it's more efficient than using regular expressions.

        Up to you proove me wrong :D


        --
        Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>

        Comment

        • Pedro Graca

          #5
          Re: preg_match digits?

          Westcoast Sheri wrote:[color=blue]
          > What is the most efficient way of extracting the first two digits in a
          > string?[/color]

          <?php
          function digits($string, $length, $pad) {
          $temp = preg_replace('=[^0123456789]=', '', $string);
          for ($i = 0; $i < $length; ++$i) $temp .= $pad;
          return substr($temp, 0, $length);
          }

          $string = 'ujdk3ca94abc';
          echo digits($string, 2, '0');
          $string = 'abc 8a bc abc934';
          echo digits($string, 2, '0');
          ?>

          --
          USENET would be a better place if everybody read: : mail address :
          http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
          http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
          http://www.expita.com/nomime.html : to 10K bytes :

          Comment

          • Westcoast Sheri

            #6
            Re: preg_match digits?

            Pedro Graca wrote:
            [color=blue]
            > Westcoast Sheri wrote:[color=green]
            > > What is the most efficient way of extracting the first two digits in a
            > > string?[/color]
            >
            > <?php
            > function digits($string, $length, $pad) {
            > $temp = preg_replace('=[^0123456789]=', '', $string);
            > for ($i = 0; $i < $length; ++$i) $temp .= $pad;
            > return substr($temp, 0, $length);
            > }
            >
            > $string = 'ujdk3ca94abc';
            > echo digits($string, 2, '0');
            > $string = 'abc 8a bc abc934';
            > echo digits($string, 2, '0');
            > ?>
            >
            > --
            > USENET would be a better place if everybody read: : mail address :
            > http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
            > http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
            > http://www.expita.com/nomime.html : to 10K bytes :[/color]

            thanks! That looks like what I was looking for. Although, I thought there
            was a "code" you could put after the preg_match part just to return the
            first 2 digits.... I guess there's not? Also, here's another question: what
            does the "6" mean in the following line of code? I know the "5" means to
            find 5 digits in a row....but what's the "6" mean?
            preg_match("/[0-9]{5,6}/",$string,$blah )


            Comment

            • Westcoast Sheri

              #7
              Re: preg_match digits?

              Tim Van Wassenhove wrote:
              [color=blue]
              > In article <40BBED21.44389 456@nospamun8no spam.com>, Westcoast Sheri wrote:[color=green]
              > > Tim Van Wassenhove wrote:
              > >[color=darkred]
              > >> In article <40BBDBB4.F0F19 AE8@nospamun8no spam.com>, Westcoast Sheri wrote:
              > >> > What is the most efficient way of extracting the first two digits in a
              > >> > string?
              > >> >
              > >> > The following is wrong for me, because it only gives me the first
              > >> > instance of two digits together:
              > >>
              > >> $found = '';
              > >>
              > >> $index = 0;
              > >>
              > >> while (strlen($found) < $needed && $index < strlen($string) ) {
              > >> if (is_numeric($st ring{$index})) {
              > >> $found .= $string{$index} ;
              > >> }
              > >> ++$index;
              > >> }[/color][/color]
              >[color=green]
              > > Tim, that's a joke, right?[/color]
              >
              > 1-) It works.
              > 2-) I presume it's more efficient than using regular expressions.
              >
              > Up to you proove me wrong :D
              >
              > --
              > Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>[/color]

              Answer:

              <?php
              $string = 'a 20adiidkk4kidid 39399399dkkdkkk dk';
              $dig = substr(preg_rep lace("/\D/",'',$string),0 ,2);
              echo $dig;
              ?>

              Takes .000027 to run.


              Comment

              • Matthias Esken

                #8
                Re: preg_match digits?

                Pedro Graca wrote:
                [color=blue]
                > Westcoast Sheri wrote:[color=green]
                >> What is the most efficient way of extracting the first two digits in a
                >> string?[/color]
                >
                > <?php
                > function digits($string, $length, $pad) {
                > $temp = preg_replace('=[^0123456789]=', '', $string);
                > for ($i = 0; $i < $length; ++$i) $temp .= $pad;
                > return substr($temp, 0, $length);
                > }
                >
                > $string = 'ujdk3ca94abc';
                > echo digits($string, 2, '0');
                > $string = 'abc 8a bc abc934';
                > echo digits($string, 2, '0');
                > ?>[/color]

                That's a very flexible function and you're my god. :-) Nevertheless, if
                you just need what the original poster asked for, you could use a simple
                regular expression:

                function digits2($string ) {
                $return = '';
                if (preg_match('~( \d)[^\d]*(\d)~', $string, $matches)) {
                $return = $matches[1] .$matches[2];
                }
                return $return;
                }

                Regards,
                Matthias

                Comment

                • Matthias Esken

                  #9
                  Re: preg_match digits?

                  Westcoast Sheri schrieb:
                  [color=blue]
                  > thanks! That looks like what I was looking for. Although, I thought there
                  > was a "code" you could put after the preg_match part just to return the
                  > first 2 digits.... I guess there's not? Also, here's another question: what
                  > does the "6" mean in the following line of code? I know the "5" means to
                  > find 5 digits in a row....but what's the "6" mean?
                  > preg_match("/[0-9]{5,6}/",$string,$blah )[/color]

                  Minimum:5, Maximum:6.

                  Regards,
                  Matthias

                  Comment

                  • Tim Van Wassenhove

                    #10
                    Re: preg_match digits?

                    In article <40BCDEDB.F664D B67@nospamun8no spam.com>, Westcoast Sheri wrote:[color=blue]
                    > Tim Van Wassenhove wrote:[color=green]
                    >> 1-) It works.
                    >> 2-) I presume it's more efficient than using regular expressions.
                    >> Up to you proove me wrong :D[/color][/color]
                    [color=blue]
                    > Answer:
                    >
                    ><?php
                    > $string = 'a 20adiidkk4kidid 39399399dkkdkkk dk';
                    > $dig = substr(preg_rep lace("/\D/",'',$string),0 ,2);
                    > echo $dig;
                    > ?>
                    >
                    > Takes .000027 to run.[/color]

                    Feel free to run the following script a few times ;)
                    test1 always had a smaller execution time than test2 when i tried....


                    <?php

                    function test1($string, $needed) {
                    $found = '';
                    $index = 0;

                    while (strlen($found) < $needed && $index < strlen($string) ) {
                    if (is_numeric($st ring{$index})) {
                    $found .= $string{$index} ;
                    }
                    ++$index;
                    }
                    return $found;
                    }

                    function test2($string) {
                    $dig = substr(preg_rep lace("/\D/",'',$string),0 ,2);
                    return $dig;
                    }


                    $strings = array(
                    '12sqdmfksdjflm qsdfjlm',
                    'sdfsqdf3mkjmkl j4',
                    'a56qsdfmqsdfkj ',
                    'sdf7dqfdsf8dqf df',
                    'a 20adiidkk4kidid 39399399dkkdkkk dk'
                    );

                    $start = microtime();
                    foreach($string s as $string) {
                    test1($string,2 );
                    }
                    $end = microtime();
                    echo '<br>total: ' . ($end - start);

                    $start = microtime();
                    foreach($string s as $string) {
                    test2($string);
                    }
                    $end = microtime();
                    echo '<br>total: ' . ($end - start);

                    ?>

                    --
                    Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>

                    Comment

                    • Andy Hassall

                      #11
                      Re: preg_match digits?

                      On 1 Jun 2004 22:33:10 GMT, Tim Van Wassenhove <euki@pi.be> wrote:
                      [color=blue]
                      >In article <40BCDEDB.F664D B67@nospamun8no spam.com>, Westcoast Sheri wrote:[color=green]
                      >> Tim Van Wassenhove wrote:[color=darkred]
                      >>> 1-) It works.
                      >>> 2-) I presume it's more efficient than using regular expressions.
                      >>> Up to you proove me wrong :D[/color][/color]
                      >[color=green]
                      >> Answer:
                      >>
                      >><?php
                      >> $string = 'a 20adiidkk4kidid 39399399dkkdkkk dk';
                      >> $dig = substr(preg_rep lace("/\D/",'',$string),0 ,2);
                      >> echo $dig;
                      >> ?>
                      >>
                      >> Takes .000027 to run.[/color]
                      >
                      >Feel free to run the following script a few times ;)
                      >test1 always had a smaller execution time than test2 when i tried....[/color]

                      Actually, there's a bug here...
                      [color=blue]
                      >$start = microtime();
                      >foreach($strin gs as $string) {
                      > test1($string,2 );
                      >}
                      >$end = microtime();
                      >echo '<br>total: ' . ($end - start);[/color]
                      ^

                      Missing $.

                      Notice: Use of undefined constant start - assumed 'start'

                      Minus isn't defined on strings, so it'll cast 'start' to a numeric type. A
                      string with no numeric characters evaluates to zero when cast to a numeric
                      type. microtime() returns a string in the following format:

                      <microseconds past second> <unix timestamp>

                      ... with a space in between.

                      Casting that to a numeric type evaluates to the first numeric part, up to the
                      space. So you end up with microseconds past the second, minus zero.

                      Unfortunately this bears no relationship to actual execution time :-(

                      Suprisingly, after fixing the bug, the preg_replace method turns out about 3x
                      faster than the loop.

                      $start = microtime(true) ;
                      foreach($string s as $string) {
                      test1($string,2 );
                      }
                      $end = microtime(true) ;
                      echo '<br>total: ' . ($t1 = $end - $start);

                      $start = microtime(true) ;
                      foreach($string s as $string) {
                      test2($string);
                      }
                      $end = microtime(true) ;
                      echo '<br>total: ' . ($t2 = $end - $start);
                      echo '<br> t1/t2 = ' . round($t1/$t2, 4) . 'x';


                      total: 0.004275
                      total: 0.001488
                      t1/t2 = 2.873x

                      --
                      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                      http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

                      Comment

                      • Tim Van Wassenhove

                        #12
                        Re: preg_match digits?

                        In article <fk1qb0p0oedj0j gfn7qbe2077akv6 b6hv8@4ax.com>, Andy Hassall wrote:[color=blue]
                        > Suprisingly, after fixing the bug, the preg_replace method turns out about 3x
                        > faster than the loop.[/color]

                        *blush* Now, that changes my vision on regular expressions being "slow".


                        --
                        Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>

                        Comment

                        • Pedro Graca

                          #13
                          Re: preg_match digits?

                          Matthias Esken wrote:[color=blue]
                          > ... and you're my god. :-)[/color]

                          LOL

                          I respectfully ask you to choose better gods ...


                          <OT LEVEL="EXTREMEL Y HIGH">
                          Like no god at all
                          </OT>

                          --
                          USENET would be a better place if everybody read: : mail address :
                          http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
                          http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
                          http://www.expita.com/nomime.html : to 10K bytes :

                          Comment

                          • Westcoast Sheri

                            #14
                            Re: preg_match digits?

                            Tim Van Wassenhove wrote:
                            [color=blue]
                            > In article <fk1qb0p0oedj0j gfn7qbe2077akv6 b6hv8@4ax.com>, Andy Hassall wrote:[color=green]
                            > > Suprisingly, after fixing the bug, the preg_replace method turns out about 3x
                            > > faster than the loop.[/color]
                            >
                            > *blush* Now, that changes my vision on regular expressions being "slow".[/color]

                            *giggle* nya nya nyaaaa nya


                            Comment

                            • Andy Hassall

                              #15
                              Re: preg_match digits?

                              On 1 Jun 2004 22:55:58 GMT, Tim Van Wassenhove <euki@pi.be> wrote:
                              [color=blue]
                              >In article <fk1qb0p0oedj0j gfn7qbe2077akv6 b6hv8@4ax.com>, Andy Hassall wrote:[color=green]
                              >> Suprisingly, after fixing the bug, the preg_replace method turns out about 3x
                              >> faster than the loop.[/color]
                              >
                              >*blush* Now, that changes my vision on regular expressions being "slow".[/color]

                              Wasn't the result I was expecting, either. Although I can see reasons for it,
                              e.g. PCRE is all compiled from C, whereas the loop is all PHP so there's more
                              overhead.

                              Given the equivalent loop and PCRE regex both in C, I'd have thought the loop
                              would win hands down.

                              --
                              Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                              http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

                              Comment

                              Working...