phpstringfiltertrim

Remove words which are less than 4 characters from a string


I need to take only full words from a string I mean full words = words with more then 4 chars. Example of a string:

"hey hello man are you going to write some code"

I need to return to:

"hello going write some code"

Also i need to trim all of these words and put them into a simple array.

Is it possible?


Solution

  • You could use a regular expression to do it.

    preg_replace("/\b\S{1,3}\b/", "", $str);
    

    You could then put them into an array with preg_split().

    preg_split("/\s+/", $str);