Uppercase String Convert [duplicate] - php

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

Related

how to echo the words after - character

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

How to make the first character of first word lowercase in PHP?

How to convert this:
"Elephant, Africa, landscape"
into this:
"elephant, Africa, landscape"
I tried using strlower and lcfirst php functions, but that is not what I wish. I wish just fist character of just first word to be lowercase, not all sentence lowercase or all words to be with first character lowercase.
Is there something to get first character of first word only lowercase?
UPDATE:
I wish to show post title as keywords, and I use this:
$title = get_the_title( $post->post_parent ); // Get post title
$parts = explode( ' ', $title ); // Delimetar for words in post title " "
$str = '';
foreach ($parts as $word) {
$str.= lcfirst(post_title_as_keywords($word)); // Lowercase first character
}
$str = substr($str, 0,-2);
echo $str;
And this is what I get:
"elephant, africa, landscape"
How to prevent lowercase effect on all words?
PHP Version: > 5.3.0:
The lcfirst() function converts the first character of a string to lowercase.
echo lcfirst("Elephant, Africa, landscape");
//output elephant, Africa, landscape
have a look at w3c schools
For PHP < 5.3 use
$your_string = "Elephant, Africa, landscape";
$your_string[0] = strtolower($str[0]);
//output elephant, Africa, landscape
UPDATE:
Use the function not inside your foreach-loop will fix your issue.
You're using lcfirst() once per word (inside the for loop). Try applying lcfirst() to str after the loop, instead of inside it:
$str = lcfirst(substr($str, 0,-2));

Regex, PHP - finding words that need correction

I have a long string with words. Some of the words have special letters.
For example a string "have now a rea$l problem with$ dolar inp$t"
and i have a special letter "$".
I need to find and return all the words with special letters in a quickest way possible.
What I did is a function that parse this string by space and then using “for” going over all the words and searching for special character in each word. When it finds it—it saves it in an array. But I have been told that using regexes I can have it with much better performance and I don’t know how to implement it using them.
What is the best approach for it?
I am a new to regex but I understand it can help me with this task?
My code: (forbiden is a const)
The code works for now, only for one forbidden char.
function findSpecialChar($x){
$special = "";
$exploded = explode(" ", $x);
foreach ($exploded as $word){
if (strpos($word,$forbidden) !== false)
$special .= $word;
}
return $special;
}
You could use preg_match like this:
// Set your special word here.
$special_word = "café";
// Set your sentence here.
$string = "I like to eat food at a café and then read a magazine.";
// Run it through 'preg_match''.
preg_match("/(?:\W|^)(\Q$special_word\E)(?:\W|$)/i", $string, $matches);
// Dump the results to see it working.
echo '<pre>';
print_r($matches);
echo '</pre>';
The output would be:
Array
(
[0] => café
[1] => café
)
Then if you wanted to replace that, you could do this using preg_replace:
// Set your special word here.
$special_word = "café";
// Set your special word here.
$special_word_replacement = " restaurant ";
// Set your sentence here.
$string = "I like to eat food at a café and then read a magazine.";
// Run it through 'preg_replace''.
$new_string = preg_replace("/(?:\W|^)(\Q$special_word\E)(?:\W|$)/i", $special_word_replacement, $string);
// Echo the results.
echo $new_string;
And the output for that would be:
I like to eat food at a restaurant and then read a magazine.
I am sure the regex could be refined to avoid having to add spaces before and after " restaurant " like I do in this example, but this is the basic concept I believe you are looking for.

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

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

How can I make the first character of a string lowercase in PHP?

I cannot use strtolower as it affects all characters. Should I use some sort of regular expression?
I'm getting a string which is a product code. I want to use this product code as a search key in a different place with the first letter made lowercase.
Try
lcfirst — Make a string's first character lowercase
and for PHP < 5.3 add this into the global scope:
if (!function_exists('lcfirst')) {
function lcfirst($str)
{
$str = is_string($str) ? $str : '';
if(mb_strlen($str) > 0) {
$str[0] = mb_strtolower($str[0]);
}
return $str;
}
}
The advantage of the above over just strolowering where needed is that your PHP code will simply switch to the native function once you upgrade to PHP 5.3.
The function checks whether there actually is a first character in the string and that it is an alphabetic character in the current locale. It is also multibyte aware.
Just do:
$str = "STACK overflow";
$str[0] = strtolower($str[0]); // prints sTACK overflow
And if you are using 5.3 or later, you can do:
$str = lcfirst($str);
Use lcfirst():
<?php
$foo = 'HelloWorld';
$foo = lcfirst($foo); // helloWorld
$bar = 'HELLO WORLD!';
$bar = lcfirst($bar); // hELLO WORLD!
$bar = lcfirst(strtoupper($bar)); // hELLO WORLD!
?>
For a multibyte first letter of a string, none of the previous examples will work.
In that case, you should use:
function mb_lcfirst($string)
{
return mb_strtolower(mb_substr($string, 0, 1)) . mb_substr($string, 1);
}
The ucfirst() function converts the first character of a string to uppercase.
Related functions:
lcfirst() - converts the first character of a string to lowercase
ucwords() - converts the first character of each word in a string to uppercase
strtoupper() - converts a string to uppercase
strtolower() - converts a string to lowercase
PHP version: 4 and later

Categories