2014-09-17 2 views
0
$variable = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf"; 

나는 그 나는 짓을했는지에 대한는 문자열의 소문자를 변경 필요 - PHP

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

[implode()] (http://php.net/manual/fr/function.implode.php) 함수는 폭발과 반대입니다. 문자열의 배열 요소를 반환합니다. –

+0

또한 'ucfirst()'를 사용하여 문자열의 첫 글자 만 대문자로 변경할 수 있습니다 : http://php.net/manual/en/function.ucfirst.php – Stefan

답변

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

정말 우아하고, 그러나 아마 조금 더 유연하지 않습니다.

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