lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
PHP: filtering words
Looking for a simple way to isolate words in a string. With apostrophes and commas and various other punctuation getting in the way, not as easy as you'd think. This works pretty well if not absolutely perfectly. An example:

# create WORDS array from string
$WORDS = explode(' ', $string);

# tidy up words
foreach ( $WORDS as $_word )
{
$WORD[] = preg_replace("/[^A-Za-z0-9'\-]*/", '', $_word);
}


keywords: regex, preg_replace, words, non-word characters