I am trying to filter out words smaller than 3 chars from a string as following:

$s = "fleece plaid rood fleece deken fleece plaid v z v borduring 220 g ma fleece";

$redundant = "/\s[^\s]{1,2}\s/";
$s = preg_replace($redundant, " ", " " . $s . " ");

First run would return:
"fleece plaid rood fleece deken fleece plaid z borduring 220 ma fleece "

And if I run it with that string again it returns:
"fleece plaid rood fleece deken fleece plaid borduring 220 fleece "

But of course I want to only run the preg_replace only once. Anyone a bright idea
what I am doing wrong here?

PS. Also tried:\s.{1,2}\s but that is giving the same result.

    Try using '\b' instead of '\s'. Note that you'll be left with extra spaces - one additional regular expression replace should take care of that (if necessary).

      Thanks B!

      That did the trick indeed. I had never heard of the \b before.

      And yes, I am doing a \s+ replace afterwards to clean it up.

        Write a Reply...