在PHP中从字符串中删除短单词和字符


Remove short words and characters from a string in PHP

我有一个字符串,可以像这样:

$searchterm = "The quick brown fox, jumps over the lazy dog! 48372. John's?"

是否有一种方法可以删除所有3个字符及以下的单词以及非字母数字字符(除了撇号)?

我希望我的结果是:

quick brown jumps over lazy 48372 John's 
$result = trim( preg_replace(
    "/[^a-z0-9']+([a-z0-9']{1,3}[^a-z0-9']+)*/i",
    " ",
    " $searchterm "
) );

顺便说一句,如果你想把单词放在数组中,有一个更简单的解决方案:

preg_match_all( "/[a-z0-9']{4,}/i", $searchterm, $words );
$words = $words[0];

当然,您可以使用implode()explode()在两种输出格式之间进行转换。

你可以这样做…

/* remove the non alphanumeric except for quotes */
$searchterm = preg_replace('/[^a-z0-9'' ]/i', '', $searchterm);
/* remove <= three letter words */
$searchterm = preg_replace('/(^| )[a-z0-9'']{,3}( |$)/i', ' ', $searchterm);