443,706 Members | 2,030 Online
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
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 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 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 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? -- 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? -- 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 Answer: 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? 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: 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.... total: ' . (\$end - start); \$start = microtime(); foreach(\$strings as \$string) { test2(\$string); } \$end = microtime(); echo '
total: ' . (\$end - start); ?> -- Tim Van Wassenhove Jul 17 '05 #10

 P: n/a On 1 Jun 2004 22:33:10 GMT, Tim Van Wassenhove 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: 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 '
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: ... 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 '
total: ' . (\$t1 = \$end - \$start); \$start = microtime(true); foreach(\$strings as \$string) { test2(\$string); } \$end = microtime(true); echo '
total: ' . (\$t2 = \$end - \$start); echo '
t1/t2 = ' . round(\$t1/\$t2, 4) . 'x'; total: 0.004275 total: 0.001488 t1/t2 = 2.873x -- Andy Hassall / 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 , 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 Jul 17 '05 #12

 P: n/a Matthias Esken wrote: ... and you're my god. :-) LOL I respectfully ask you to choose better gods ... Like no god at all -- 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 , 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 wrote: In article , 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 / 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.