Uppercase for first letter with php Uppercase for first letter with php codeigniter codeigniter

Uppercase for first letter with php


In this particular string example, you could explode the strings first, use that function ucfirst() and apply to all exploded strings, then put them back together again:

$string = 'title-title-title';$strings = implode('-', array_map('ucfirst', explode('-', $string)));echo $strings;

Should be fairly straightforward on applying this:

$title = '';if($this->session->userdata('head_title') != '') {    $raw_title = $this->session->userdata('head_title'); // title-title-title    $title = implode('-', array_map('ucfirst', explode('-', $raw_title)));} else {    $title = 'Our Home Page';}echo $title;


echo str_replace(" ","-",ucwords(str_replace("-"," ","title-title-title")));

Fiddle

Output:

Title-Title-Title


Demo

Not as swift as Ghost's but a touch more readable for beginners to see what's happening.

//break words on delimiter$arr = explode("-", $string);//capitalize first word only$ord = array_map('ucfirst', $arr);//rebuild the stringecho implode("-", $ord);

The array_map() applies callback to the elements of the given array. Internally, it traverses through the elements in our word-filled array $arr and applies the function ucfirst() to each of them. Saves you couple of lines.