(PHP) making string lowercase , with exception of first letter [duplicate] - php

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
?>

Related

Get and replace words in string that contains ? in between of word in string php regex

how does i use php regex to achieve the following :
i have a string that contains :
$a = "Does Foo loves Bar ? i don?t think so, Foo doesn?t. Okay?";
and i want to get output of :
Does Foo loves Bar ? i don't think so, Foo doesn't. Okay?
with all the ? in between of text replaced with single quote (') .
i have tried the following code but the Okay? is also being replaced with Okay' :
<?php
$str = "This is Agbeniga , he is a very good boy. He?s loved , but is he really is ? No , i don?t think so. but it?s working very good anyway , ya?ll all darling. Okay?";
$str = str_replace("?","0",$str);
//replace all question marks with zero
$divide=explode(" ",$str);
$str="";
foreach($divide as $div)
{
if(substr($div,0,1)===0)
{
$div=preg_replace("/0/", "?",$div);
}
$str .= ' ';
$str.=$div;
}
//split by spaces and replace words that have zero at the end with question mark
$str= preg_replace('/\b(?<!-)\d+(?!-)\b/',"?",$str);
//replace all stand alone zeros with question mark
$str = str_replace("0","'",$str);
//then replace the remaining zeros with single quote
echo $str;
?>
Regex can check that on both sides of question mark boundaries of words are placed (`\b')
$str = "This is Agbeniga , he is a very good boy. He?s loved , but is he really is ? No , i don?t think so. but it?s working very good anyway , ya?ll all darling. Okay?";
$str= preg_replace('/\b\?\b/',"'",$str);
echo $str;
demo

Making the first character of every sentence capital case [duplicate]

This question already has answers here:
How to capitalize first letter of first word in a sentence?
(7 answers)
Closed 6 years ago.
I have a function that make the character of every new sentence upper case, however, it's not working properly. It only works if the new word is right up against the punctuation mark, and not if there is a space after the punctuation mark. How would I fix this?
//****************************************************************
function ucAll($str) {
return preg_replace_callback('/(?<=^|[\.\?!])[^\.]/', function ($match) {
return strtoupper($match[0]);
}, $str);
} //end of function ucAll($str)
//****************************************************************
$string = "i dont' want to? why should i?";
$string = ucAll($string);
echo $string;
Result
I dont' want to? why should i?
Need Result
I dont' want to? Why should i?
Just add (\s)* in appropriate place of your regular expression
<?php
//****************************************************************
function ucAll($str) {
return preg_replace_callback('/(?<=^|[\.\?!])(\s)*[^\.]/', function ($match) {
return strtoupper($match[0]);
}, $str);
} //end of function ucAll($str)
//****************************************************************
$string = "i dont' want to? why should i?";
$string = ucAll($string);
echo $string;

Uppercase String Convert [duplicate]

This question already has answers here:
Make all words lowercase and the first letter of each word uppercase
(3 answers)
Closed 1 year ago.
I have strings from a source code and its like;
"REAL MADRİD - BARCELONA"
"VİLLAREAL - IDIONA"
"FENERBAHÇE - BEŞİKTAŞ IŞ"
etc.. etc.. etc..
I want to convert this string totally lowercase and uppercase first character of each word. (like ucwords). But i have a problem with İI and other utf-8 characters when using ucwords function.
I want exactly output for this strings like;
"Real Madrid - Barcelona"
"Villareal - Idıona"
"Fenerbahçe - Beşiktaş Iş"
What can i try?
Thanks.
I found this functiom:
function mb_ucwords($str)
{
return mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
}
The function you need is mb_strtolower()
Usage: echo mb_strtolower($str);
For more details, read the Manual.
Need to use strtolower and ucwords
Example :
$string = "REAL MADRİD - BARCELONA";
$lower = strtolower($string); //first need to convert to lower string
$firstupper = ucwords($lower); //then change every word first letter is upper case
You should use ucwords with strtolower php method combination
$s = "REAL MADRID - BARCELONA";
$s = ucwords(strtolower($s));
#Real Madrid - Barcelona
Check out this fiddle
For utf8 strings instead of strtolower you can use mb_convert_case method with MB_CASE_TITLE mode like:
$s = "FENERBAHÇE - BEŞİKTAŞ IŞ";
$s = mb_convert_case($s, MB_CASE_TITLE, "UTF-8");
Check out this fiddle
please notice that the last one will work for sure also if the first letter of word is special utf8 kind
You can try this:
$string = "REAL MADRİD - BARCELONA";
mb_internal_encoding('UTF-8');
$finalString = mb_convert_case($string, MB_CASE_TITLE);
echo $finalString;
Result:
Real Madrid - Barcelona
try this
$string = "REAL MADRID - BARCELONA";
$ans = ucwords(strtolower ( $string ));
I guess you figured it out, you should use:
mb_strtolower
and
mb_convert_case
Here:
$games = array(
"REAL MADRİD - BARCELONA",
"VİLLAREAL - IDIONA",
"FENERBAHÇE - BEŞİKTAŞ IŞ",
"İSTANBULSPOR A.Ş - FENERBAHÇE İŞ"
);
foreach($games as $game) {
echo mb_convert_case($game, MB_CASE_TITLE, "UTF-8") . '<br>';
}
it will print the result you're looking for:
Real Madrid - Barcelona
Villareal - Idiona
Fenerbahçe - Beşiktaş Iş
İstanbulspor A.ş - Fenerbahçe İş
To capitalize like A.S a solution would be:
foreach($games as $game) {
$tmp = str_replace(".",". ",$game);
echo mb_convert_case($tmp, MB_CASE_TITLE, "UTF-8") . '<br>';
}
and you get:
Real Madrid - Barcelona
Villareal - Idiona
Fenerbahçe - Beşiktaş Iş
İstanbulspor A. Ş - Fenerbahçe İş
Of course, you may create your own function, especially if there are other requirements as well.
Try this:
<?php
echo ucwords(strtolower('Dhaka, JAMALPUR, sarishabari'));
Result is:
Dhaka, Jamalpur, Sarishabari

How to make the first letter of a word uppercase? [duplicate]

This question already has answers here:
Uppercase first letter and rest lower
(6 answers)
Closed 7 years ago.
I have this word:
katii
But I want the first character in uppercase K. How can I do this?
Use function usfirst
string ucfirst ( string $str )
You can use below as prescribed by php.net
<?php
$foo = 'hello world!';
$foo = ucfirst($foo); // Hello world!
$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?>
$upper = strtoupper(substr('katii', 0, 1)); //K
You should make your question a little clearer in future, though.

make uppercase the first letter of name [duplicate]

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;
?>

Categories