$pattern = \"#([$delimiters])#\";<\/pre>\nThen you get an array containing all the elements, according to our regex:<\/p>\n
$chunks = preg_split($pattern, $name,-1, PREG_SPLIT_DELIM_CAPTURE);<\/pre>\nUsing the flat PREG_SPLIT_DELIM_CAPTURE, allows use to keep the delimiters inside the\u00a0chunks <\/em>array.<\/p>\nCapitalize everything !<\/h3>\n To finish, on every element cut, pass the chain into lowercase using\u00a0strtolower<\/em> then capitalize the first letter with\u00a0ucfirst<\/em>.<\/p>\nforeach ($chunks as &$n) {\r\n $n = ucfirst(strtolower($n));\r\n}<\/pre>\nWe’re using here the character & within our\u00a0foreach <\/em>loop. It keeps the variable\u00a0$n <\/em>as a reference within the array $chunks. If the variable $n is updated then so is the corresponding line in the\u00a0chunks<\/em> array.<\/p>\nThen, the last step consists into reassembling the array into a string :<\/p>\n
$name = implode('',$chunks);\r\n\r\nreturn $name;<\/pre>\nFINAL PHP FUNCTION :<\/h2>\n Here is the final version of the function we just created :<\/p>\n
\/**\r\n *\r\n * This function allows you to add a capitalize letter to every letter of a word.\r\n *\r\n * @Exemple : \"Jean-MARIE De la rue\" will become \"Jean-Marie De La Rue\"\r\n *\r\n * @param string|null $name\r\n * @param string $delimiters\r\n *\r\n * @return string|null\r\n *\/\r\nfunction uc_first($name,$delimiters = \" -\")\r\n{\r\n \/\/ Return null name is not defined\r\n if(null === $name) {\r\n return $name;\r\n }\r\n\r\n \/\/ Here we use \"-\" and \" \" as separators. You can add every separator you want\r\n $pattern = \"#([$delimiters])#\";\r\n\r\n \/\/ Explode the string linked according to the pattern\r\n $chunks = preg_split($pattern, $name,-1, PREG_SPLIT_DELIM_CAPTURE);\r\n\r\n \/\/ On every chunk, we first put everything lowercase then uppercase the 1st character\r\n foreach ($chunks as &$n) {\r\n $n = ucfirst(strtolower($n));\r\n }\r\n\r\n \/\/ We re-link everything together\r\n $name = implode('',$chunks);\r\n\r\n return $name;\r\n}<\/pre>\nThe code is available with a MIT license on github in the repository xusifob\/lib<\/a><\/p>\nDon’t hesitate to leave a comment below !<\/p>\n","protected":false},"excerpt":{"rendered":"
in this post, we will see how to capitalize every first letter of a sentence, or a name. This post if the 3rd of my serie on PHP useful functions. You can find my other posts about Importing a CSV file in PHP and Export a PHP array in a CSV File. This function is […]<\/p>\n","protected":false},"author":1,"featured_media":515,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,23,22],"tags":[],"translation":{"provider":"WPGlobus","version":"2.12.2","language":"en","enabled_languages":["fr","en","es"],"languages":{"fr":{"title":true,"content":true,"excerpt":false},"en":{"title":true,"content":true,"excerpt":false},"es":{"title":false,"content":false,"excerpt":false}}},"acf":[],"yoast_head":"\n
Capitalize the first letter of every word in PHP - Bastien Malahieude<\/title>\n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n\t \n\t \n\t \n