Php 对于也包含数字的子字符串,以不同的方式更改字符串中的大小写

Php 对于也包含数字的子字符串,以不同的方式更改字符串中的大小写,php,string,case,Php,String,Case,我需要改变PHP中字符串的大小写,改变的方式取决于子字符串 如果子字符串由空格和字母分隔,则第一个字母大写,其余字母小写。 如果有字母和数字,它们仍应保留大写: 这是PHP5 ucwords(strtolower("NEW APPLE IPHONE X 64GB CVX-Dk46")) 例如: 新款苹果IPHONE X 64GB CVX-Dk46 应成为: 新款苹果Iphone X 64GB CVXDk46 这将循环遍历每个单词,查看单词中是否有数字,如果没有,则执行strtolower和uc

我需要改变PHP中字符串的大小写,改变的方式取决于子字符串

如果子字符串由空格和字母分隔,则第一个字母大写,其余字母小写。
如果有字母和数字,它们仍应保留大写:

这是PHP5

ucwords(strtolower("NEW APPLE IPHONE X 64GB CVX-Dk46"))
例如:

新款苹果IPHONE X 64GB CVX-Dk46

应成为:

新款苹果Iphone X 64GB CVXDk46


这将循环遍历每个单词,查看单词中是否有数字,如果没有,则执行strtolower和ucwords

$str = "NEW APPLE IPHONE X 64GB CVX-Dk46";

$arr = explode(" ", $str); // make it array

foreach($arr as &$word){ // loop array
    if(!preg_match("/\d/", $word)){ // is there not a digit in the word
        $word = ucwords(strtolower($word));
    }
}

echo implode(" ", $arr); // implode array to string
//New Apple Iphone X 64GB CVX-Dk46

这将循环遍历每个单词,查看单词中是否有数字,如果没有,则执行strtolower和ucwords

$str = "NEW APPLE IPHONE X 64GB CVX-Dk46";

$arr = explode(" ", $str); // make it array

foreach($arr as &$word){ // loop array
    if(!preg_match("/\d/", $word)){ // is there not a digit in the word
        $word = ucwords(strtolower($word));
    }
}

echo implode(" ", $arr); // implode array to string
//New Apple Iphone X 64GB CVX-Dk46

这里是另一种方法。唯一的区别是在Andreas的回答中使用了
array\u walk()
函数而不是
foreach()
循环。(这也是一个很好的答案。)


这是另一种方法。唯一的区别是在Andreas的回答中使用了
array\u walk()
函数而不是
foreach()
循环。(这也是一个很好的答案。)


你不能用一行代码来实现这一点。如果对你有帮助,请参阅下面的代码

$val = "NEW APPLE IPHONE X 64GB CVX-Dk46";
$val = explode(" ", $val);
$finalString = '';
foreach ($val as $value) {
 if(preg_match('/^[a-zA-Z]+[a-zA-Z0-9._]+$/', $value)) { 
   $finalString = $finalString . " " . ucwords(strtolower($value));
 } else {
 $finalString = $finalString . " " . $value;
 }
}
echo $finalString;  
输出如下:-

New Apple Iphone X 64GB CVX-Dk46

你不能用一行代码来实现这一点。如果对你有帮助,请参阅下面的代码

$val = "NEW APPLE IPHONE X 64GB CVX-Dk46";
$val = explode(" ", $val);
$finalString = '';
foreach ($val as $value) {
 if(preg_match('/^[a-zA-Z]+[a-zA-Z0-9._]+$/', $value)) { 
   $finalString = $finalString . " " . ucwords(strtolower($value));
 } else {
 $finalString = $finalString . " " . $value;
 }
}
echo $finalString;  
输出如下:-

New Apple Iphone X 64GB CVX-Dk46

首先,您需要在字符串中找到数字 -如果有需要将字符串分隔到数组的数字 第一个数组是仅包含字符串,第二个数组是包含数字(或数字和字符串) -如果没有所需的数字,则需要使用php函数strtolower降低字符串,并使用php函数ucwords将字符串的第一个字符转换为大写 您可以尝试以下代码: 链接:
函数大写字符串($string)
{
$pattern='/(?=\d)/';
$array=preg_split($pattern,$string,2);
$text='';
如果(计数($array)>1)
{
$text=ucwords(strtolower($array[0])。'.strtoupper($array[1]);
}
其他的
{
$text=ucwords(strtolower($array[0]);
}
返回$text;
}
$str=“新苹果IPHONE X 64GB CVX-Dk46”;

回显大写字符串($str)首先,您需要在字符串中查找数字 -如果有需要将字符串分隔到数组的数字 第一个数组是仅包含字符串,第二个数组是包含数字(或数字和字符串) -如果没有所需的数字,则需要使用php函数strtolower降低字符串,并使用php函数ucwords将字符串的第一个字符转换为大写 您可以尝试以下代码: 链接:
函数大写字符串($string)
{
$pattern='/(?=\d)/';
$array=preg_split($pattern,$string,2);
$text='';
如果(计数($array)>1)
{
$text=ucwords(strtolower($array[0])。'.strtoupper($array[1]);
}
其他的
{
$text=ucwords(strtolower($array[0]);
}
返回$text;
}
$str=“新苹果IPHONE X 64GB CVX-Dk46”;

回显大写字符串($str)我刚刚注意到最后一个单词中的
-
缺失,这是打字错误还是应该删除?如果是,删除
-
的标准是什么?我刚刚注意到最后一个单词中的
-
缺失,这是打字错误还是应该删除?如果是,删除
-
的标准是什么?