需要更改字符串的大小写-PHP
我需要在$variable上面显示需要更改字符串的大小写-PHP,php,arrays,for-loop,case-sensitive,Php,Arrays,For Loop,Case Sensitive,我需要在$variable上面显示 $variable = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf"; 为此,我做了: Test Company Insurance LLC Chennai Limited W-8TYU.pdf $variable=str\u replace(“\uu”,“test\u company\u insurance\u llc\u chennai\u limited\u w-8tyu.pdf”);
$variable = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf";
为此,我做了:
Test Company Insurance LLC Chennai Limited W-8TYU.pdf
$variable=str\u replace(“\uu”,“test\u company\u insurance\u llc\u chennai\u limited\u w-8tyu.pdf”);
$test=explode(“,$variable);
$countof=计数($test);
对于($x=0;$x来说,它不是非常优雅,但可能稍微灵活一些
$str_in = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf";
$lst_in = explode("_", $str_in);
$lst_out = array();
foreach ($lst_in as $val) {
switch($val) {
case "llc" : $lst_out[] = strtoupper($val);
break;
case "w-8tyu.pdf" : $lst_temp = explode('.', $val);
$lst_out[] = strtoupper($lst_temp[0]) . "." . $lst_temp[1];
break;
default : $lst_out[] = ucfirst($val);
}
}
$str_out = implode(' ', $lst_out);
echo $str_out;
如果将扩展与初始值分开,则可以删除忽略。不是非常优雅,但可能稍微灵活一些
$str_in = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf";
$lst_in = explode("_", $str_in);
$lst_out = array();
foreach ($lst_in as $val) {
switch($val) {
case "llc" : $lst_out[] = strtoupper($val);
break;
case "w-8tyu.pdf" : $lst_temp = explode('.', $val);
$lst_out[] = strtoupper($lst_temp[0]) . "." . $lst_temp[1];
break;
default : $lst_out[] = ucfirst($val);
}
}
$str_out = implode(' ', $lst_out);
echo $str_out;
如果将扩展名与初始值分开,则可以删除忽略项。请参阅下面的代码。我已将代码输出打印为您预期的输出。因此,请运行它并回复我
$v = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");
$acronyms = array('llc', 'w-8tyu');
$ignores = array('pdf');
$v = preg_replace_callback('/(?:[^\._\s]+)/', function ($match) use ($acronyms, $ignores) {
if (in_array($match[0], $ignores)) {
return $match[0];
}
return in_array($match[0], $acronyms) ? strtoupper($match[0]) : ucfirst($match[0]);
}, $v);
echo $v;
$variable=str\u replace(“\uu”,“test\u company\u insurance\u llc\u chennai\u limited\u w-8tyu.pdf”);
$test=explode(“,$variable);
$countof=计数($test);
对于($x=0;$xgetExtension();
}
否则{
$test[$x]=ucfirst($test[$x]);
}
}
回声';
打印(测试);
回声';
echo$output=内爆(“,$test”);
查看下面的代码。我已将代码输出打印为您期望的输出。请运行它并回复我
$v = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");
$acronyms = array('llc', 'w-8tyu');
$ignores = array('pdf');
$v = preg_replace_callback('/(?:[^\._\s]+)/', function ($match) use ($acronyms, $ignores) {
if (in_array($match[0], $ignores)) {
return $match[0];
}
return in_array($match[0], $acronyms) ? strtoupper($match[0]) : ucfirst($match[0]);
}, $v);
echo $v;
$variable=str\u replace(“\uu”,“test\u company\u insurance\u llc\u chennai\u limited\u w-8tyu.pdf”);
$test=explode(“,$variable);
$countof=计数($test);
对于($x=0;$xgetExtension();
}
否则{
$test[$x]=ucfirst($test[$x]);
}
}
回声';
打印(测试);
回声';
echo$output=内爆(“,$test”);
该函数与explode相反:它将数组的元素粘在一起作为字符串。您也可以使用ucfirst()
仅更改字符串中第一个字母的大小写:该函数与explode相反:它将数组的元素粘在一起作为字符串。您还可以使用ucfirst()
要仅更改字符串中第一个字母的大小写,请执行以下操作: