php - How can I match characters in a string and place a dot next to each character using regex -


please see code below.

this returns john doe l. r t

i want 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 $match) {     $string = str_replace($match, $match.'.',$string); }  echo $string; 

you can use preg_replace that

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

the result

john doe l. r. t.

and you'll want replace uppercase-letters (since names), should not use i flag.

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

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -