массивы — необходимо изменить регистр строки

$variable = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf";

Мне нужно отобразить над переменной $, как

Test Company Insurance LLC Chennai Limited W-8TYU.pdf

Для этого я сделал:

$variable = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");

$test  = explode(" ", $variable);
$countof = count($test);

for ($x=0; $x<$countof; $x++) {

if($test[$x] == 'w-8tyu' || $test[$x] == 'llc') {
$test[$x] = strtoupper($test[$x]);
//todo
}

}

Я застрял в делах.

Я заменим определенные слова на заглавные, используя strtoupper.

Позже, как мне нужно объединить массив?

Любая помощь будет благодарна …

0

Решение

$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;
3

Другие решения

Не очень элегантно, но, возможно, немного более гибко.

$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;

Игнорирование может быть удалено при условии, что вы отделяете расширение от начального значения.

1

Смотрите код ниже. Я напечатал вывод кода как ожидаемый. Так что запустите и ответьте мне …

$variable = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");

$test  = explode(" ", $variable);
$countof = count($test);

for ($x=0; $x<$countof; $x++) {

if($test[$x] == 'llc') {
$test[$x] = strtoupper($test[$x]);
//todo
}elseif($test[$x] == 'w-8tyu.pdf'){
$file=basename($test[$x],'pdf');
$info = new SplFileInfo($test[$x]);
$test[$x] = strtoupper($file).$info->getExtension();
}
else{
$test[$x]=ucfirst($test[$x]);
}
}
echo '<pre>';
print_r($test);
echo '</pre>';
echo $output  = implode(" ", $test);
1
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector