PHP: function to remove/replace all single characters in a string?

Solution:1

You could use regex:

$str = trim(preg_replace('@(^|\pZ)\pL\pM*(?=\pZ|$)@u', ' ', $str));

This would remove any single letter. If you want to remove any character period, you could do:

$str = trim(preg_replace('@(^|\pZ)P\Z(?=\pZ|$)@u', ' ', $str));

Demo: http://codepad.viper-7.com/YaLUQD

Solution:2

$string = "A Quick Brown B C D Fox";
$array = explode(' ', $string);
foreach ($array as $key => $value)
    if (strlen($value) == 1)
        unset($array[$key]);
$string = implode(" ", $array);
echo $string;