By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,706 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,706 IT Pros & Developers. It's quick & easy.

preg_match digits?

P: n/a
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.

Jul 17 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
In article <40***************@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>
Jul 17 '05 #2

P: n/a
Tim Van Wassenhove wrote:
In article <40***************@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?


Jul 17 '05 #3

P: n/a
In article <40***************@nospamun8nospam.com>, Westcoast Sheri wrote:
Tim Van Wassenhove wrote:
In article <40***************@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>
Jul 17 '05 #4

P: n/a
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 :
Jul 17 '05 #5

P: n/a
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)
Jul 17 '05 #6

P: n/a
Tim Van Wassenhove wrote:
In article <40***************@nospamun8nospam.com>, Westcoast Sheri wrote:
Tim Van Wassenhove wrote:
In article <40***************@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.
Jul 17 '05 #7

P: n/a
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
Jul 17 '05 #8

P: n/a
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
Jul 17 '05 #9

P: n/a
In article <40***************@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>
Jul 17 '05 #10

P: n/a
On 1 Jun 2004 22:33:10 GMT, Tim Van Wassenhove <eu**@pi.be> wrote:
In article <40***************@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....


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

^

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($strings as $string) {
test1($string,2);
}
$end = microtime(true);
echo '<br>total: ' . ($t1 = $end - $start);

$start = microtime(true);
foreach($strings 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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #11

P: n/a
In article <fk********************************@4ax.com>, Andy Hassall wrote:
Suprisingly, after fixing the bug, the preg_replace method turns out about 3x
faster than the loop.


*blush* Now, that changes my vision on regular expressions being "slow".
--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Jul 17 '05 #12

P: n/a
Matthias Esken wrote:
... and you're my god. :-)


LOL

I respectfully ask you to choose better gods ...
<OT LEVEL="EXTREMELY 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 :
Jul 17 '05 #13

P: n/a
Tim Van Wassenhove wrote:
In article <fk********************************@4ax.com>, Andy Hassall wrote:
Suprisingly, after fixing the bug, the preg_replace method turns out about 3x
faster than the loop.


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


*giggle* nya nya nyaaaa nya
Jul 17 '05 #14

P: n/a
On 1 Jun 2004 22:55:58 GMT, Tim Van Wassenhove <eu**@pi.be> wrote:
In article <fk********************************@4ax.com>, Andy Hassall wrote:
Suprisingly, after fixing the bug, the preg_replace method turns out about 3x
faster than the loop.


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


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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.