Saturday, July 4th, 2009

Extract alphanumeric sequences from a string

by Gabriel on 05/10/08 at 6:42 am

Save to StumbleUpon Stumble Upon it!     Save to Del.icio.us Save to Del.icio.us    Share on Twitter! Share on Twitter!

Greetings! Subscribe to my RSS feed or get my latest post directly in your mailbox. Thanks for visiting!

This short snippet is useful if you need to extract alphanumeric sequences (characters) from a string:

function extract_alphanumeric_sequences($string)
{
preg_match_all('/([a-zA-Z0-9]+)/', $string, $match);

return $match[0];
}

$string = 'Suspendisse 354 65pretium eros ut 43564 mauris. Integer in lacus quis est dignissim posuere. Aenean dui.  &$#^@ %$ 94375  Mauris non turpis sit amet nunc imperdiet varius.';

$alpha_array = extract_alphanumeric_sequences($string);

echo '<pre>'; print_r($alpha_array); echo "</pre>";

Output:

Array
(
    [0] => Suspendisse
    [1] => 354
    [2] => 65pretium
    [3] => eros
    [4] => ut
    [5] => 43564
    [6] => mauris
    [7] => Integer
    [8] => in
    [9] => lacus
    [10] => quis
    [11] => est
    [12] => dignissim
    [13] => posuere
    [14] => Aenean
    [15] => dui
    [16] => 94375
    [17] => Mauris
    [18] => non
    [19] => turpis
    [20] => sit
    [21] => amet
    [22] => nunc
    [23] => imperdiet
    [24] => varius
)
Be notified when we have new posts by subscribing to BitRepository RSS Feed.
Support us!Did you like this post?
Please spread the word!
Save to StumbleUpon  Save to Del.icio.us  Share on Twitter!    

Leave a Comment