This question already has answers here:
How to make first letter of a word capital?
(5 answers)
Closed 7 years ago.
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.
Try with -
$fullname1 = ucwords(str_replace('-', ' ', $fullname1));
You can use the ucword function
Here's an example:
<!DOCTYPE html>
<html>
<body>
<?php
echo ucwords("hello world");
?>
</body>
</html>
<?php $text = str_replace("-", " ", $fullname1);
echo ucwords($text);?>
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));
<?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;
?>
Related
I have code where I am extracting a name from a database, trying to reorder the word, and then changing it from all uppercase to word case. Everything I find online suggests my code should work, but it does not... Here is my code and the output:
$subjectnameraw = "SMITH, JOHN LEE";
$subjectlname = substr($subjectnameraw, 0, strpos($subjectnameraw, ",")); // Get the last name
$subjectfname = substr($subjectnameraw, strpos($subjectnameraw, ",") + 1) . " "; // Get first name and middle name
$subjectname = ucwords(strtolower($subjectfname . $subjectlname)); // Reorder the name and make it lower case / upper word case
However, the output looks like this:
John Lee smith
The last name is ALWAYS lowercase no matter what I do. How can I get the last name to be uppercase as well?
The above code gives wrong results when there are multibyte characters in the names like RENÉ. The following solution uses the multibyte function mb_convert_case.
$subjectnameraw = "SMITH, JOHN LEE RENÉ";
list($lastName,$firstnName) = explode(', ',mb_convert_case($subjectnameraw,MB_CASE_TITLE,'UTF-8'));
echo $firstnName." ".$lastName;
Demo : https://3v4l.org/ekTQA
i have a page and its showing all the names with "-" character between them
and i want to just echo the first word before the - character and the second word after the - character
arsenal - liverpool
Betis - Athletic Bilbao
Waasland Beveren - Oostende
the code i tired
$arr1 = explode(' ',trim($Event["Event"]["name"]));
echo $arr1[0]."\n";
but this is just showing the first word , if the first word includes tow names then i cant echo it , i want to echo the full name before and after the - character in tow seprate php
i want to get this outputs from the examples
arsenal
liverpool
Betis
Athletic Bilbao
Waasland Beveren
Oostende
Try this:
$arr1 = explode('-', trim($Event["Event"]["name"]));
$team = $arr1[0];
$town = $arr1[1];
echo "$team - $town";
You are using the explode function with the wrong argument value, it should be used with a hyphen (-) instead of a space.
Note this will leave you with leading and trailing spaces, to remove this you can use the trim function:
$team = trim($arr1[0]);
$town = trim($arr1[1]);
Or put a space before and after the hyphen in the explode function:
$arr1 = explode(' - ', trim($Event["Event"]["name"]));
This is only the case if your string will always be in the format:
<team> - <town>
The explode() function breaks a string into an array.
Note: The "separator" parameter cannot be an empty string.
Use "-" separator in explode function.
<?php
$arr1 = explode('-',trim($Event["Event"]["name"]));
echo trim($arr1[0])."\n";
echo trim($arr1[1])."\n";
?>
This question already has answers here:
Get the first letter of each word in a string
(27 answers)
Closed 8 years ago.
I have a Person Name in a variable $contactnamefield. Example: Justin Cruise.
I want to pick the first character of Justin 'J' and C of Cruise and then combine it and Save it in a variable $username = 'JC'.
<?php
$name = "John A Doe";
$parts = explode(' ',$name);
$initials = '';
foreach($parts as $part) {
$initials .= $part[0];
}
echo $initials;
?>
This should work. It gets the first letter of each part, not just the first and last. I'm not sure if this is exactly what you want since you only gave a name clause of first and last name, not sure how you want to handle middle names.
This question already has answers here:
Make all words lowercase and the first letter of each word uppercase
(3 answers)
Closed 8 years ago.
I've made a simple textbox which leads to a new page which echo's the input word by word (explode).
Now i want to to change all the letters to lowercase except the first letter (if this is inputted this way)
for example : Herman Archer LIVEs in NeW YORK --> Herman Archer lives in new york
I hope you can help me out , thanks in advance!
$str = 'stRing';
ucfirst(strtolower($str));
Will output: String
Just do
ucfirst(strtolower($string)); //Would output "Herman archer lives in new york"
Also if you wanted every word to start with a captial you could do
ucwords(strtolower($string)); //Would output "Herman Archer Lives In New York"
To make all chars of a string lowercase except the first, use:
echo $word[0] . strtolower(substr($word, 1));
Use mb_convert_case, but you have to create an equivalent to ucfirst:
<?php
function mb_ucfirst($string) {
$string = mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
return $string;
}
$string = 'hEllO wOrLD';
$string = mb_ucfirst(mb_convert_case($string, MB_CASE_LOWER));
echo $string; // Prints: Hello world
?>
I had a strange coding situation where I needed to have the URI become the title of the page being viewed. I couldn't think of another way to do it but now I need to format that URI and can't figure out how to accomplish it. It is a WordPress site so the URI is pretty clean. What I want to do is capitalize the letter of the first word and then a space, dash or pipe delimiter to separate out the title.
So this obviously gives me the URI:
<title><?php echo ($_SERVER['REQUEST_URI']) ?></title>
which gives me something like /test-catalog/diagnosis/flu. What I want to display is Test Catalog - Diagnosis - Flu
Thank you.
I would imagine this would work:
echo ucwords(str_replace(Array("-","/"),Array(" "," - "),$_SERVER['REQUEST_URI']);
By using str_replace and ucwords
Example
echo ucwords(str_replace('/', ' - ', str_replace('-', ' ', $_SERVER['REQUEST_URI'])));
several things to do:
$url = str_replace("-"," ",$url); // convert the - to spaces (do this first)
$url = str_replace("/"," - ",$url); // convert the / to hyphens with spaces either side
$title = ucfirst($url); // capitalize the first letter
if you want to capitalize each letter, then do this:
$title = ucwords($url); // capitalize first letter of each word
you might have some whitepsace begining and end, so do this:
$title= trim($title)
// remove the first slash '/'
$uri = substr($_SERVER['REQUEST_URI'], 1);
// ucwords to uppercase any word
// str_replace to replace "-" with " " and "/" with " - "
echo ucwords(str_replace(array("-","/"),array(" "," - "),$uri));
codepad
as a résumé of the previous answers:
echo ucwords(str_replace(array("-","/"),array(" "," - "),substr($_SERVER['REQUEST_URI'], 1)));