让我们说我们想在一些字符串中用大写字母表示大写:
johndev
johnasp
johnphp
johnserver
我们将此用于此目的:
ucfirst(str_replace($name,ucfirst($name),$result['@attributes']['overflows']))
john是我们的$ name变量。它的工作原理如下:
Johndev //** these also should be in uppercase, for example : JohnDev
Johnasp
Johnphp
Johnserver
DevJohn
AspJohn
PhpJohn
ServerJohn
我该如何解决这个问题?
答案 0 :(得分:1)
我的解决方案是:
echo
ucfirst($name)
. ucfirst(substr($result['@attributes']['overflows'], strlen($name)));
你在这做什么:
ucfirst
$name
本身ucfirst
$result['@attributes']['overflows']
的{{1}}部分
对于像'PhpJohn'这样的字符串,只需交换部分:
$name
答案 1 :(得分:1)
假设您有$whole
和$john
,$name
始终是$whole
的前缀。
$whole = "johndev";
$name = "john";
$capName = ucfirst($name); // "John"
$tail = substr($whole, strlen($name)); // "dev"
$capTail = ucfirst($tail); // "Dev"
echo $capName . $capTail; // "JohnDev"
如果$name
可能出现在$whole
的任何位置,那么您可以使用:
$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;
答案 2 :(得分:1)
最后,我找到了解决此问题的方法:
$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);