I have this as a string-
$String ="A. Bird Blue B. Red Hat C. Purple Dinosaur D. Black hat E. Clean soap";
I want this -
$output="A. Bird Blue, B. Red Hat, C. Purple Dinosaur, D. Black hat, E. Clean soap";
Please help me with this.
I have this as a string-
$String ="A. Bird Blue B. Red Hat C. Purple Dinosaur D. Black hat E. Clean soap";
I want this -
$output="A. Bird Blue, B. Red Hat, C. Purple Dinosaur, D. Black hat, E. Clean soap";
Please help me with this.
You can try using regex.
$String ="A. Bird Blue B. Red Hat C. Purple Dinosaur D. Black hat E. Clean soap";
$regex = '/(\s[A-Z]\.)/';
$Output = preg_replace($regex, ',$1', $String);
echo $Output;
Output:
A. Bird Blue, B. Red Hat, C. Purple Dinosaur, D. Black hat, E. Clean soap
Regex Explanation:
/(\s[A-Z]\.)/
\s
match any white space character
[A-Z] match a single character present in the list below A-Z a single character in the range between A and Z (case sensitive)
\.
matches the character .
literallyThe matching is replaced by [comma + first group (\s[A-Z]\.)
].