» Birthday Bundle - Over $400 worth of Envato files for just $20

PHP: Extract Alphanumeric Sequences from a String

Posted on October 5, 2008, Filled under PHP,  Bookmark it

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
)

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)