如何匹配字符串中的字符,并使用正则表达式在每个字符旁边放置一个点

Please see my code below.

this returns John Doe L. R T

I want it to return John Doe L. R. T.

$string = "John Doe L R T";

$matches = null;
preg_match('/\b\s[a-zA-z]{1}\b/i', $string, $matches);

foreach ($matches as $match)
{
    $string = str_replace($match, $match.'.',$string);
}

echo $string;

You can use preg_replace for that

preg_replace('/\b\s[a-zA-z]{1}\b/i', '$0.', $string);

The result would be

John Doe L. R. T.

And you'll probably want to replace that for uppercase-letters only (since they are likely to be names), so you should not use the i flag.

preg_replace('/\b\s[A-z]{1}\b/', '$0.', $string);

You could probably just combine the operations using preg_replace(). Something like this should work:

$string = preg_replace('/\b([a-zA-Z])\b/', '$1.', $string);

Notice you have two lowercase z in your character class, so if you fix that you don't need the i modifier. Also, no need to specify {1}.