Programming Tips - PHP: convert a string to an array of English words (in php)

Date: 2010apr20 Language: php Q. PHP: convert a string to an array of English words (in php) A. Here is a function that does that:
function getWords(string $a): array { return preg_split('/\W+/', $a, -1, PREG_SPLIT_NO_EMPTY); } # One problem: turns apostrophes into two words - eg "Apple's iPad" # becomes ["Apple", "s", "iPad"] so an alternative is: function getWords(string $a): array { return preg_split('/[\s,;\-\.]+/', $a, -1, PREG_SPLIT_NO_EMPTY); } function exampleUse() { $str = "One two three."; $words = getWords($str); foreach ($words as $word) { print "word=$word\n"; } }