I have the following function, I want it to strip "alpha" when is a single word only and not a part of a composite word like "alphadog". Now instead I just see "dog" and it's not good. Any help?
function stripwords($string)
{
// build pattern once
static $pattern = null;
if ($pattern === null) {
// pull words to remove from somewhere
$words = array('alpha', 'beta', '-');
// escape special characters
foreach ($words as &$word) {
$word = preg_quote($word, '#');
}
// combine to regex
$pattern = '#\b(' . join('|', $words) . ')\b\s*#iS';
}
$print = preg_replace($pattern, '', $string);
list($firstpart)=explode('+', $print);
return $firstpart;
}
edit: hi, i have another problem... i've edited above with the new version of the function: it strips words, adjust whitespaces and then does something else i need, but it doesn't remove dashes (or minus)... what's wrong? i tried something but no avail...thanks
This:
Should be this:
Use this regexp pattern
/\balpha\b/
\b
stands for "word boundary", it means it will match the word by separate (if the word is surrounded by word separators).Hope this helps