Reputation: 313
Let's say we want to uppercase the first letter in some strings like :
johndev
johnasp
johnphp
johnserver
and we use this for such purpose :
ucfirst(str_replace($name,ucfirst($name),$result['@attributes']['overflows']))
john is our $name variable. It works like this :
Johndev //** these also should be in uppercase, for example : JohnDev
Johnasp
Johnphp
Johnserver
DevJohn
AspJohn
PhpJohn
ServerJohn
How can I fix this?
Upvotes: 0
Views: 55
Reputation: 313
Finally, I found a way to fix this issue :
$thekey = str_replace($name, '', $result['@attributes']['overflow']);
$ucname = str_replace($name,ucfirst($name),$result['@attributes']['overflow']);
$thename = str_replace($thekey,ucfirst($thekey),$ucname);
echo ucfirst($thename);
Upvotes: 1
Reputation: 333
Assuming you have $whole
and $john
, and $name
is always the prefix of $whole
.
$whole = "johndev";
$name = "john";
$capName = ucfirst($name); // "John"
$tail = substr($whole, strlen($name)); // "dev"
$capTail = ucfirst($tail); // "Dev"
echo $capName . $capTail; // "JohnDev"
If $name
could appear anywhere in $whole
for any number of times, then you can use:
$whole = "phpjohndevjohnjava";
$name = "john";
$parts = explode($name, $whole); // ["php", "dev", "java"]
$capParts = array_map(ucfirst, $parts); // ["Php", "Dev", "Java"]
$capName = ucfirst($name); // "John"
$answer = implode($capName, $capParts); // "PhpJohnDevJohnJava"
echo $answer;
Upvotes: 1
Reputation: 54796
My solution is:
echo
ucfirst($name)
. ucfirst(substr($result['@attributes']['overflows'], strlen($name)));
Here what you do:
ucfirst
$name
itselfucfirst
part of $result['@attributes']['overflows']
which comes after $name
For strings like 'PhpJohn' just swap parts:
echo
ucfirst(substr($result['@attributes']['overflows'], strlen($name)))
. ucfirst($name);
Upvotes: 1