PHP: Extract Alphabetical Sequences from a String
Posted on October 5, 2008, Filled under PHP,
Bookmark it
This function extracts alphabetical sequences (usually words) from a string (text):
function extract_alphabetical_sequences($string)
{
preg_match_all('/([a-zA-Z]+)/', $string, $match);
return $match[0];
}
$string = 'In hac habitasse platea dictumst. 54 8*5 5#$&*^@ $ @ In eget sem. Etiam quam. Nam nec, justo sed lorem, auctor tincidunt. Nulla id risus laoreet nisl *^$&&@#$ egestas ultrices.';
$alpha_array = extract_alphabetical_sequences($string);
echo '<pre>'; print_r($alpha_array); echo "</pre>";
Output:
Array
(
[0] => In
[1] => hac
[2] => habitasse
[3] => platea
[4] => dictumst
[5] => In
[6] => eget
[7] => sem
[8] => Etiam
[9] => quam
[10] => Nam
[11] => nec
[12] => justo
[13] => sed
[14] => lorem
[15] => auctor
[16] => tincidunt
[17] => Nulla
[18] => id
[19] => risus
[20] => laoreet
[21] => nisl
[22] => egestas
[23] => ultrices
)
If you need to extract all the words from a string, consider using the str_word_count() function.
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- October 5, 2008
- article by Gabriel C.
- Leave a reply!
Sponsors
Related Posts
-
PHP: Extract Alphanumeric Sequences from a Stringat October 5, 2008
-
PHP: How to extract numbers from a string (text)at October 5, 2008 with 1 comment
-
Shorten a string (text)at September 3, 2008 with 2 comments
-
How to emphasize specific words from a string (text)at September 6, 2008
-
How to extract username from an e-mail address stringat September 5, 2008 with 2 comments
