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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 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? |
|
|||
|
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> |
|
|||
|
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 : |
|
|||
|
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) |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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> |