Remove non-alphanumeric characters (white space and multibyte characters versions also) and remove words shorter than 3 characters
<?php
function removeChars( $string ) {
// keep white space
return preg_replace('/[^a-zA-Z0-9\s]/', '', $string);
// remove white space
return preg_replace('/(W)+/', '', $string);
// remove words shorter than 3 characters - doesn't work with multibyte characters (š, č...)
return preg_replace('~b[a-z]{1,2}bs*~', '', $string);
}
// remove words shorter than 3 characters - works with multibyte characters (š, č...)
$maxWordLength = 3;
$string = "Volvo en če da BMW ti motor 77 še Toyota";
$exploded = explode(" ", $string);
foreach($exploded as $key => $word) {
if(mb_strlen($word) < $maxWordLength) unset($exploded[$key]);
}
$string = implode(" ", $exploded);
echo $string;