1

I have a variable like

$fullname = "dwayne-johnson";

How can I make the "d" of the first letter of the word dwayne and the "j" of the word johnson? like I want to make uppercase the first letter of the every word that is separated by dash, for example I have following variable (refer below)

$fullname1 = "dwayne-johnson" //expected result is, Dwayne Johnson
$fullname2 = "maria-osana-makarte" //expected result is, Maria Osana Makarte

as you can see from above the variable fullname1 have 2 words separated by dash and so the first letter in both words are capitalized. The second variable $fullname2 has 3 words separated by dash and so the first letter of each words within that variable is been capitalized. So how to make it capitalize the first letter of each words that is separated by dash in variable? Any clues, ideas, suggestions, help and recommendations is greatly appreciated. Thank you.

PS: i have already a function that convert the dash to space so now all I have to do is to make the first letter of every words that is separated by dash within the variable and the after it has been capitalized I will then inject the dash-space function on it.

Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164

5 Answers5

2

Try with -

$fullname1 = ucwords(str_replace('-', ' ', $fullname1));
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0
    <?php $text = str_replace("-", " ", $fullname1);
echo ucwords($text);?>
Vivek Singh
  • 2,453
  • 1
  • 14
  • 27
0

You can use the ucword function

Here's an example:

<!DOCTYPE html>
<html>
<body>

<?php
echo ucwords("hello world");
?>  

</body>
</html>
Phoenix
  • 335
  • 2
  • 9
0

You can use the following code

$fullname = "dwayne-johnson";
// replace dash by space
$nospaces = str_replace("-", " ", $fullname);
// use ucword to capitalize the first letter of each word, 
// making sure that the input string is fully lowercase
$name = ucword(strtolower($nospaces));
mark
  • 344
  • 3
  • 8
0
<?php
$fullname = "dwayne-johnson";
$fullname_without_dash = str_replace("-"," ",$fullname); // gives -> "dwayne johnson"
$fullname_ucword = ucwords($fullname_without_dash); //gives -> "Dwayne Johnson"
echo $fullname_ucword;
?>
Aneesh R S
  • 3,807
  • 4
  • 23
  • 35