Converting Php String To Title Case


Answer :

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


Use Stringy
enter image description here



composer.json



"require": {
"voku/stringy": "~5.0"
}


PHP



Stringy::create('string')->toTitleCase()


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable