[英]How uppercase words with space and "-" between
I would like to change a string with我想用
hello teSt woRld hEllo-woRld
to至
Hello Test World Hello-World
I did something, that observes only spaces between the words.我做了一些事情,只观察单词之间的空格。 But I need it for spaces and "-" between.
但我需要它用于空格和“-”之间。 Can someone tell me how to achieve it?
有人可以告诉我如何实现吗?
$words = explode(" ", $string);
foreach ( $words as $key=>$value ) {
$new .= mb_strtoupper( mb_substr( mb_strtolower( $value) , 0, 1, 'UTF-8'), 'UTF-8' ) .
mb_substr(mb_strtolower( $value ), 1, null, 'UTF-8') . ' ';
}
you should try something like this:你应该尝试这样的事情:
$string = "hello teSt woRld hEllo-woRld";
echo ucwords(strtolower($string), " -");
The second parameter of the ucwords
function is the word delimiter. ucwords
function 的第二个参数是单词分隔符。 In this case, words are separated with either a space or a hyphen.在这种情况下,单词用空格或连字符分隔。
The result will be:结果将是:
Hello Test World Hello-World
If you are concerned about keeping the UTF-8 format, you should consider using the mb_convert_case
function like the example below:如果您担心保留 UTF-8 格式,则应考虑使用
mb_convert_case
function,如下例所示:
$str = "mary had a Little lamb and she loved it so";
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
echo $str; // Display: Mary Had A Little Lamb And She Loved It So
You could also do it with a regex您也可以使用正则表达式
$str = 'hello teSt woRld hEllo-woRld';
$str = strtolower($str);
$line = preg_replace_callback(
'(^\w|\s\w|\-\w)',
function ($matches) {
return strtoupper($matches[0]);
},
$str
);
echo $line;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.