开发者

Remove short words and characters from a string in PHP

I h开发者_Python百科ave a string which could be like this:

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

Is there a way to remove all words that are 3 characters and under as well as characters that aren't alphanumeric (except for apostrophes)?

I want my result to be:

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 "
) );

BTW, if you want the words in an array, there's an even simpler solution:

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

Of course, you can use implode() and explode() to convert between the two output formats.


You could do this..

/* 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜