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.
Related
This question already has answers here:
Using PHP replace regex with regex
(3 answers)
Closed 3 years ago.
I'm trying something like this -
$foo = "foo555bar25foobar1";
if(1 === preg_match('~[0-9]~', $foo, $matches) {
$bar = preg_replace($matches, "-".$matches."-", $foo);
};
echo $bar;
The result I'm looking to achieve is:
foo-555-bar-25-foobar-1-
$foo = "foo555bar25foobar1";
$bar = preg_replace('/\d+/', '-$0-', $foo);
echo $bar;
This question already has answers here:
How substr function works? [closed]
(3 answers)
Closed 4 years ago.
I am trying to get "pa" from "a(bcdefghijkl(mno)pa)q".
This is my code for exampe:
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,15);
echo $mystring;
outputs is:
mno)pa)q
You have use right code but the second parameter is wrong. use this one below
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,2);
echo $mystring;
In substr function:
first parameter means from which position of string starts.
and the second parameter means how many characters you have want.
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,-4,2);
echo $mystring;
Try this
This question already has answers here:
How to extract only part of string in PHP?
(3 answers)
Closed 5 years ago.
I have a string in a variable like this:
$var = "This is a banana (fruit)";
How do I trim / get the part 'fruit' only? (i.e., data inside braces whatever it is).
Try this:
<?php
$sentence = "This is a banana (fruit)";
$a = stripos($sentence,"(");
$b = stripos($sentence,")");
echo substr($sentence, $a, $b);
?>
Use preg_rpelace function:
<?php
$str = "This is a banana (fruit)";
echo preg_replace('/.*\((.*)\).*/','$1',$str);
?>
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
?>
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Include constant in string without concatenating
How can I get a constant to be evaluated in a string like variables are?
$foo = "like";
define('BAR', 'fish');
// None of these give me 'I like to eat fish on Tuesday.'
echo "I $foo to eat BAR on Tuesday.";
echo "I $foo to eat {BAR} on Tuesday.";
echo 'I $foo to eat BAR on Tuesday.';
// This works but is undesirable
echo 'I $foo to eat '.BAR.' on Tuesday.';
The only way I know this'd works (which you may already be aware of) is by doing:
<?php
$foo = "like";
define('BAR', 'fish');
$constants = get_defined_constants();
echo "I $foo to eat {$constants['BAR']} on Tuesday.";
?>
which prints:
I like to eat fish on Tuesday.
As far as I know that isn't possibile. Check this answer here at SO, it contains useful workarounds if concatenation is so undesirable too you! Include constant in string without concatenating
of course there is no way. And no need.