Converting php string to header
I want to convert an input string to a header.
So if I have an input line
Name: MR. M.A.D KARIM
I want to create the following line of output
Name: M.A.D Karim
And if I have an input line
Address: 12/A, ROOM NO-B 13
I want to create
Address: 12/A, ROOM NO-B 13
I want my output string had a capital letter after any space characters, as well as after any of the following characters: .
, -
, /
.
My current solution
ucwords(strtolower($string));
But it leaves the characters after .
, -
and /
lowercase, while I want them to be uppercase.
+4
source to share
2 answers
This should work for you:
<?php
$str = "Name: MR. M.A.D KARIM";
$result = "";
$arr = array();
$pattern = '/([;:,-.\/ X])/';
$array = preg_split($pattern, $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach($array as $k => $v)
$result .= ucwords(strtolower($v));
//$result = str_replace("Mr.", "", $result); ->If you don't want Mr. in a String
echo $result;
?>
Input:
Name: MR. M.A.D KARIM
Address: 12/A, ROOM NO-B 13
Output:
Name: M.A.D Karim
Address: 12/A, Room No-B 13
+2
source to share