{"id":512,"date":"2020-05-05T14:57:06","date_gmt":"2020-05-05T13:57:06","guid":{"rendered":"https:\/\/bastienmalahieude.fr\/?p=512"},"modified":"2020-05-05T17:37:26","modified_gmt":"2020-05-05T16:37:26","slug":"php-premiere-lettre-des-mots-en-majuscule","status":"publish","type":"post","link":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/","title":{"rendered":"Capitalize the first letter of every word in PHP"},"content":{"rendered":"<p>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 <a href=\"https:\/\/bastienmalahieude.fr\/en\/importer-un-fichier-csv-en-tableau-php\/\">Importing a CSV file in PHP<\/a> and <a href=\"https:\/\/bastienmalahieude.fr\/en\/export-array-php-csv-microsoft-excel\/\">Export a PHP array in a CSV File<\/a>.<\/p>\n<p>This function is used to transform a string such as <strong>Jean-MARIE De la rue<\/strong> into <strong>Jean-Marie De La Rue<\/strong><\/p>\n<p>For this, we will create a custom fonction\u00a0<em>uc_first<\/em> that takes the name of the user as a parameter and output a formatted string. This functions takes two parameters, but only the first one is required :<\/p>\n<ul>\n<li><strong>$name<\/strong> : This parameter is the name you wish to transform<\/li>\n<li>$<strong>delimiters<\/strong> : The delimiters you wish to use, they separate words. By default, the function is using the dash &#8220;-&#8221; et the space &#8221; &#8220;<\/li>\n<\/ul>\n<pre> function uc_first($name)\r\n{\r\n\/\/ @TODO Do something here\r\n}<\/pre>\n<p>In a first time, we will make sure that the name passed is not\u00a0<em>null<\/em>. This step is not required, but can help you some troubles.<\/p>\n<pre>if(null === $name) {\r\n    return $name;\r\n}<\/pre>\n<h3>Description of the process<\/h3>\n<p>Then we will follow two distinct steps :<\/p>\n<ul>\n<li>First, cut the string in multiple chunks, using the delimiters set previously. This step will use the function\u00a0<em>preg_split<\/em> and a regular expression.<\/li>\n<li>Then, we will take every chunk and capitalize their first letter.<\/li>\n<\/ul>\n<p>The first step is to create the regexp pattern. <a href=\"https:\/\/regex101.com\/\" target=\"_blank\" rel=\"noopener\">More information on Regular expressions is available here<\/a>.<\/p>\n<pre>$pattern = \"#([$delimiters])#\";<\/pre>\n<p>Then you get an array containing all the elements, according to our regex:<\/p>\n<pre>$chunks = preg_split($pattern, $name,-1, PREG_SPLIT_DELIM_CAPTURE);<\/pre>\n<p>Using the flat PREG_SPLIT_DELIM_CAPTURE, allows use to keep the delimiters inside the\u00a0<em>chunks <\/em>array.<\/p>\n<h3>Capitalize everything !<\/h3>\n<p>To finish, on every element cut, pass the chain into lowercase using\u00a0<em>strtolower<\/em> then capitalize the first letter with\u00a0<em>ucfirst<\/em>.<\/p>\n<pre>foreach ($chunks as &amp;$n) {\r\n    $n = ucfirst(strtolower($n));\r\n}<\/pre>\n<p>We&#8217;re using here the character &amp; within our\u00a0<em>foreach <\/em>loop. It keeps the variable\u00a0<em>$n <\/em>as a reference within the array $chunks. If the variable $n is updated then so is the corresponding line in the\u00a0<em>chunks<\/em> array.<\/p>\n<p>Then, the last step consists into reassembling the array into a string :<\/p>\n<pre>$name = implode('',$chunks);\r\n\r\nreturn $name;<\/pre>\n<h2>FINAL PHP FUNCTION :<\/h2>\n<p>Here is the final version of the function we just created :<\/p>\n<pre>\/**\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 &amp;$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>\n<p>The code is available with a MIT license on github in the repository <a href=\"https:\/\/github.com\/Xusifob\/lib\">xusifob\/lib<\/a><\/p>\n<p>Don&#8217;t hesitate to leave a comment below !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":515,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[20,23,22],"tags":[],"class_list":["post-512","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-freelance","category-php","category-wordpress"],"translation":{"provider":"WPGlobus","version":"3.0.0","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":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Capitalize the first letter of every word in PHP - Bastien Malahieude<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Capitalize the first letter of every word in PHP - Bastien Malahieude\" \/>\n<meta property=\"og:url\" content=\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/\" \/>\n<meta property=\"og:site_name\" content=\"Bastien Malahieude\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-05T13:57:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-05T16:37:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/bastienmalahieude.fr\/wp-content\/uploads\/2020\/05\/csm_papiers-SIRE_b12524b5f3.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"819\" \/>\n\t<meta property=\"og:image:height\" content=\"383\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"bastien\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"bastien\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/\"},\"author\":{\"name\":\"bastien\",\"@id\":\"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/59ad42a2add75f0f9af8e06f493841c7\"},\"headline\":\"Capitalize the first letter of every word in PHP\",\"datePublished\":\"2020-05-05T13:57:06+00:00\",\"dateModified\":\"2020-05-05T16:37:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/\"},\"wordCount\":820,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/59ad42a2add75f0f9af8e06f493841c7\"},\"articleSection\":[\"Freelance\",\"PHP\",\"WordPress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/\",\"url\":\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/\",\"name\":\"Capitalize the first letter of every word in PHP - Bastien Malahieude\",\"isPartOf\":{\"@id\":\"https:\/\/bastienmalahieude.fr\/#website\"},\"datePublished\":\"2020-05-05T13:57:06+00:00\",\"dateModified\":\"2020-05-05T16:37:26+00:00\",\"description\":\"How to capitalize every words in a sentence in PHP ? Find here a function helping you to capitalize user names or sentences.\",\"breadcrumb\":{\"@id\":\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/bastienmalahieude.fr\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/bastienmalahieude.fr\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Capitalize the first letter of every word in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/bastienmalahieude.fr\/#website\",\"url\":\"https:\/\/bastienmalahieude.fr\/\",\"name\":\"Bastien Malahieude\",\"description\":\"Growth Hacker - Web Developer - Project Manager\",\"publisher\":{\"@id\":\"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/59ad42a2add75f0f9af8e06f493841c7\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/bastienmalahieude.fr\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/59ad42a2add75f0f9af8e06f493841c7\",\"name\":\"bastien\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/bastienmalahieude.fr\/wp-content\/uploads\/2017\/10\/bastien-3-carre-2.png\",\"contentUrl\":\"https:\/\/bastienmalahieude.fr\/wp-content\/uploads\/2017\/10\/bastien-3-carre-2.png\",\"width\":\"404\",\"height\":\"404\",\"caption\":\"bastien\"},\"logo\":{\"@id\":\"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Capitalize the first letter of every word in PHP - Bastien Malahieude","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/","og_locale":"en_US","og_type":"article","og_title":"Capitalize the first letter of every word in PHP - Bastien Malahieude","og_url":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/","og_site_name":"Bastien Malahieude","article_published_time":"2020-05-05T13:57:06+00:00","article_modified_time":"2020-05-05T16:37:26+00:00","og_image":[{"width":819,"height":383,"url":"https:\/\/bastienmalahieude.fr\/wp-content\/uploads\/2020\/05\/csm_papiers-SIRE_b12524b5f3.jpg","type":"image\/jpeg"}],"author":"bastien","twitter_card":"summary_large_image","twitter_misc":{"Written by":"bastien","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/#article","isPartOf":{"@id":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/"},"author":{"name":"bastien","@id":"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/59ad42a2add75f0f9af8e06f493841c7"},"headline":"Capitalize the first letter of every word in PHP","datePublished":"2020-05-05T13:57:06+00:00","dateModified":"2020-05-05T16:37:26+00:00","mainEntityOfPage":{"@id":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/"},"wordCount":820,"commentCount":0,"publisher":{"@id":"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/59ad42a2add75f0f9af8e06f493841c7"},"articleSection":["Freelance","PHP","WordPress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/","url":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/","name":"Capitalize the first letter of every word in PHP - Bastien Malahieude","isPartOf":{"@id":"https:\/\/bastienmalahieude.fr\/#website"},"datePublished":"2020-05-05T13:57:06+00:00","dateModified":"2020-05-05T16:37:26+00:00","description":"How to capitalize every words in a sentence in PHP ? Find here a function helping you to capitalize user names or sentences.","breadcrumb":{"@id":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/bastienmalahieude.fr\/en\/php-premiere-lettre-des-mots-en-majuscule\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/bastienmalahieude.fr\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/bastienmalahieude.fr\/category\/php\/"},{"@type":"ListItem","position":3,"name":"Capitalize the first letter of every word in PHP"}]},{"@type":"WebSite","@id":"https:\/\/bastienmalahieude.fr\/#website","url":"https:\/\/bastienmalahieude.fr\/","name":"Bastien Malahieude","description":"Growth Hacker - Web Developer - Project Manager","publisher":{"@id":"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/59ad42a2add75f0f9af8e06f493841c7"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/bastienmalahieude.fr\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/59ad42a2add75f0f9af8e06f493841c7","name":"bastien","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/image\/","url":"https:\/\/bastienmalahieude.fr\/wp-content\/uploads\/2017\/10\/bastien-3-carre-2.png","contentUrl":"https:\/\/bastienmalahieude.fr\/wp-content\/uploads\/2017\/10\/bastien-3-carre-2.png","width":"404","height":"404","caption":"bastien"},"logo":{"@id":"https:\/\/bastienmalahieude.fr\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/comments?post=512"}],"version-history":[{"count":5,"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/posts\/512\/revisions\/522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/media\/515"}],"wp:attachment":[{"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bastienmalahieude.fr\/en\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}