How to extract numbers from a string (text)
by Gabriel on 05/10/08 at 6:17 am
Greetings! Subscribe to my RSS feed or get my latest post directly in your mailbox. Thanks for visiting!
This is a short function that extracts numbers from a string:
function extract_numbers($string)
{
preg_match_all('/([\d]+)/', $string, $match);
return $match[0];
}
$string = 'Lorem ipsum dolor sit 45 40 amet, consectetuer adipiscing elit. 35 65675 Suspendisse sed nibh non diam consectetuer pharetra. Morbi ultricies 235 536pede et pede. 9432 3536 Nunc eu risus eget quam lacinia feugiat. In sapien sem, fringilla quis, 34 24 8762condimentum id, bibendum ut, nibh. Quisque 2367 784 elementum massa 350 235 vel nulla.';
$numbers_array = extract_numbers($string);
echo '<pre>'; print_r($numbers_array); echo "</pre>";
Output:
Array
(
[0] => 45
[1] => 40
[2] => 35
[3] => 65675
[4] => 235
[5] => 536
[6] => 9432
[7] => 3536
[8] => 34
[9] => 24
[10] => 8762
[11] => 2367
[12] => 784
[13] => 350
[14] => 235
)
Be notified when we have new posts by subscribing to
BitRepository RSS Feed.
BitRepository RSS Feed.Please spread the word! |
|
Leave a Comment