【发布时间】:2011-04-05 01:18:34
【问题描述】:
$q = 'durham-region';
$q = ucfirst($q);
$q = 'Durham-region';
如何将破折号后的字母大写(达勒姆地区)?我是否必须将两者分开并大写?
【问题讨论】:
-
mb_convert_case()3v4l.org/YOWZs
标签: php
$q = 'durham-region';
$q = ucfirst($q);
$q = 'Durham-region';
如何将破折号后的字母大写(达勒姆地区)?我是否必须将两者分开并大写?
【问题讨论】:
mb_convert_case()3v4l.org/YOWZs
标签: php
更新的解决方案
从 PHP 5.5 开始,preg_replace 的 e 修饰符已被弃用。现在最好的选择是使用不使用这个的建议之一,例如:
$q = preg_replace_callback('/(\w+)/g', create_function('$m','return ucfirst($m[1]);'), $q)
或
$q = implode('-', array_map('ucfirst', explode('-', $q)));
原答案
您可以通过这种方式使用e 修饰符来使用preg_replace:
$test = "durham-region";
$test = preg_replace("/(\w+)/e","ucfirst('\\1')", $test);
echo $test;
// Durham-Region
【讨论】:
$test = "wärme-test"; 导致类似“WäRme-Test”的结果。换句话说,元音变音之后的字母也会大写。
create_function 在 PHP 7.4 中也被弃用了
感谢ucwords的delimiter参数,从PHP 5.4.32和5.5.16开始,就这么简单:
$string = ucwords($string, "-");
【讨论】:
不使用e PCRE 修饰符的单线:
$str = implode('-', array_map('ucfirst', explode('-', $str)));
【讨论】:
另一个单行:
str_replace(' ','',ucwords(str_replace('-',' ',$action)))
【讨论】:
是的。 ucfirst() 只是将字符串的第一个字母大写。如果要将多个字母大写,则必须创建多个字符串。
$strings = explode("-", $string);
$newString = "";
foreach($strings as $string){
$newString += ucfirst($string);
}
function ucfirst_all($delimiter, $string){
$strings = explode("-", $string);
$newString = "";
foreach($strings as $string){
$newString += ucfirst($string);
}
return $newString;
}
【讨论】:
您可以使用这样的正则表达式回调方法:
$q = preg_replace_callback('/\-([a-z]+)/g', create_function(
'$m', 'return "-" . ucfirst($m[1]);'
),$q)
【讨论】:
请务必注意,此处提供的解决方案不适用于 UTF-8 字符串!
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει-υπέρ νωθρού κυνός";
$str = explode('-', mb_convert_case( $str, MB_CASE_TITLE ) );
$str = implode('-', array_map('mb_convert_case', $str, array(MB_CASE_TITLE, "UTF-8")) );
echo $str;
// str= Τάχιστη Αλώπηξ Βαφήσ Ψημένη Γη, Δρασκελίζει-Υπέρ Νωθρού Κυνόσ
【讨论】:
mb_convert_case() expects parameter 2 to be int, string given
看
function UpperCaseAfterDash($wyraz)
{
$rozbij = explode('-',$wyraz);
echo $rozbij[0].'-'.
ucfirst($rozbij[1]);
}
UpperCaseAfterDash("input-text");
以上函数返回input-Text
如果您只需要一个破折号后的大写字母,例如城市名称 (Jastrzębie-Zdrój) 就足够了,但如果您需要多个...,只需计算数组元素的数量(在上述代码中分解后) ) 存在,然后使用循环。
你好,
【讨论】:
这是一个通用函数,可让您在任何字符或字符串后大写。在提问者询问的具体情况下,针就是破折号。
function capitalizeAfter( $needle, $haystack ) {
$haystack = str_replace( $needle . "a", $needle . "A", $haystack );
$haystack = str_replace( $needle . "b", $needle . "B", $haystack );
$haystack = str_replace( $needle . "c", $needle . "C", $haystack );
$haystack = str_replace( $needle . "d", $needle . "D", $haystack );
$haystack = str_replace( $needle . "e", $needle . "E", $haystack );
$haystack = str_replace( $needle . "f", $needle . "F", $haystack );
$haystack = str_replace( $needle . "g", $needle . "G", $haystack );
$haystack = str_replace( $needle . "h", $needle . "H", $haystack );
$haystack = str_replace( $needle . "i", $needle . "I", $haystack );
$haystack = str_replace( $needle . "j", $needle . "J", $haystack );
$haystack = str_replace( $needle . "k", $needle . "K", $haystack );
$haystack = str_replace( $needle . "l", $needle . "L", $haystack );
$haystack = str_replace( $needle . "m", $needle . "M", $haystack );
$haystack = str_replace( $needle . "n", $needle . "N", $haystack );
$haystack = str_replace( $needle . "o", $needle . "O", $haystack );
$haystack = str_replace( $needle . "p", $needle . "P", $haystack );
$haystack = str_replace( $needle . "q", $needle . "Q", $haystack );
$haystack = str_replace( $needle . "r", $needle . "R", $haystack );
$haystack = str_replace( $needle . "s", $needle . "S", $haystack );
$haystack = str_replace( $needle . "t", $needle . "T", $haystack );
$haystack = str_replace( $needle . "u", $needle . "U", $haystack );
$haystack = str_replace( $needle . "v", $needle . "V", $haystack );
$haystack = str_replace( $needle . "w", $needle . "W", $haystack );
$haystack = str_replace( $needle . "x", $needle . "X", $haystack );
$haystack = str_replace( $needle . "y", $needle . "Y", $haystack );
$haystack = str_replace( $needle . "z", $needle . "Z", $haystack );
return $haystack;
}
【讨论】: