preg_match digits?

This is a discussion on preg_match digits? within the PHP Language forums, part of the PHP Programming Forums category; What is the most efficient way of extracting the first two digits in a string? The following is wrong for ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-01-2004
Westcoast Sheri
 
Posts: n/a
Default 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,$result);
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.



Reply With Quote
  #2 (permalink)  
Old 06-01-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: preg_match digits?

In article <40BBDBB4.F0F19AE8@nospamun8nospam.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($string{$index})) {
$found .= $string{$index};
}
++$index;
}


--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Reply With Quote
  #3 (permalink)  
Old 06-01-2004
Westcoast Sheri
 
Posts: n/a
Default Re: preg_match digits?

Tim Van Wassenhove wrote:

> In article <40BBDBB4.F0F19AE8@nospamun8nospam.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($string{$index})) {
> $found .= $string{$index};
> }
> ++$index;
> }
>
> --
> Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>


Tim, that's a joke, right?




Reply With Quote
  #4 (permalink)  
Old 06-01-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: preg_match digits?

In article <40BBED21.44389456@nospamun8nospam.com>, Westcoast Sheri wrote:
> Tim Van Wassenhove wrote:
>
>> In article <40BBDBB4.F0F19AE8@nospamun8nospam.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($string{$index})) {
>> $found .= $string{$index};
>> }
>> ++$index;
>> }


> Tim, that's a joke, right?


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>
Reply With Quote
  #5 (permalink)  
Old 06-01-2004
Pedro Graca
 
Posts: n/a
Default Re: preg_match digits?

Westcoast Sheri wrote:
> What is the most efficient way of extracting the first two digits in a
> string?


<?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 :
Reply With Quote
  #6 (permalink)  
Old 06-01-2004
Westcoast Sheri
 
Posts: n/a
Default Re: preg_match digits?

Pedro Graca wrote:

> Westcoast Sheri wrote:
> > What is the most efficient way of extracting the first two digits in a
> > string?

>
> <?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 :


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)


Reply With Quote
  #7 (permalink)  
Old 06-01-2004
Westcoast Sheri
 
Posts: n/a
Default Re: preg_match digits?

Tim Van Wassenhove wrote:

> In article <40BBED21.44389456@nospamun8nospam.com>, Westcoast Sheri wrote:
> > Tim Van Wassenhove wrote:
> >
> >> In article <40BBDBB4.F0F19AE8@nospamun8nospam.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($string{$index})) {
> >> $found .= $string{$index};
> >> }
> >> ++$index;
> >> }

>
> > Tim, that's a joke, right?

>
> 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>


Answer:

<?php
$string = 'a 20adiidkk4kidid39399399dkkdkkkdk';
$dig = substr(preg_replace("/\D/",'',$string),0,2);
echo $dig;
?>

Takes .000027 to run.


Reply With Quote
  #8 (permalink)  
Old 06-01-2004
Matthias Esken
 
Posts: n/a
Default Re: preg_match digits?

Westcoast Sheri schrieb:

> 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)


Minimum:5, Maximum:6.

Regards,
Matthias
Reply With Quote
  #9 (permalink)  
Old 06-01-2004
Matthias Esken
 
Posts: n/a
Default Re: preg_match digits?

Pedro Graca wrote:

> Westcoast Sheri wrote:
>> What is the most efficient way of extracting the first two digits in a
>> string?

>
> <?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');
> ?>


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
Reply With Quote
  #10 (permalink)  
Old 06-02-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: preg_match digits?

In article <40BCDEDB.F664DB67@nospamun8nospam.com>, Westcoast Sheri wrote:
> Tim Van Wassenhove wrote:
>> 1-) It works.
>> 2-) I presume it's more efficient than using regular expressions.
>> Up to you proove me wrong :D


> Answer:
>
><?php
> $string = 'a 20adiidkk4kidid39399399dkkdkkkdk';
> $dig = substr(preg_replace("/\D/",'',$string),0,2);
> echo $dig;
> ?>
>
> Takes .000027 to run.


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($string{$index})) {
$found .= $string{$index};
}
++$index;
}
return $found;
}

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


$strings = array(
'12sqdmfksdjflmqsdfjlm',
'sdfsqdf3mkjmklj4',
'a56qsdfmqsdfkj',
'sdf7dqfdsf8dqfdf',
'a 20adiidkk4kidid39399399dkkdkkkdk'
);

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

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

?>

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:43 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0