2013-08-23 8 views
0

나는 많은 단어를 가진 배열을 가지고 있지만, 일부 문장은 대문자이다. 예를 들면 :PHP 배열에서 대문자를 찾는 방법

THIS SENTENCE IS UPPERCASE

this sentece is in lowercase

그리고 \r\n하여이 두 문장을 분할 할,하지만 내가하는 방법을 찾을 수 없습니다 그것을

다음

내가이 배열을 가져 오지 방법은 다음과 같습니다

$result_pk_info = array(); 
    while ($row = odbc_fetch_array($sql_pk_info)) 
    { 
     $opisanie = iconv("cp1257", "UTF-8", trim($row['opis'])); 
     $id = iconv("cp1257", "UTF-8", trim($row['pacientid'])); 
     $date = iconv("cp1257", "UTF-8", trim($row['data'])); 

     $desc = explode('@#$', $opisanie); 

     $all_info = "<tr><td>".$desc[1]."</td></tr>"; 

     $result_pk_info[] = $all_info; 
    } 

$desc 나는 단어 배열을 가지고있어 내가 검색하고 분할하고 싶다. 대문자 및 소문자.

아무도 도와 줄 수 있습니까?

UPD 나는이 같은 구조 하세 뭔가가 텍스트 : SENTENCE IN UPPERCASE Sentece in lower case

+1

는 http://php.net/manual/en/function.ctype-upper.php 전체 단어를 대문자 – cptnk

+0

인가 또는 낮은뿐만 아니라 일부 상위/부분이 될 수 있는가? –

답변

1

이 기능은 당신이 찾고있는 무엇

$string = 'HI how ARE you doing ?'; 
echo split_upper_lower ($string); 

출력 : 나는 당신의 질문을 이해하면

HI 
how 
ARE 
you doing ? 
+0

하지만 난'] VAIROGDZIEDZERA
UN VIRSPUSĒJI
NOVIETOTO ORGĀNU 미국 Vairogdziedzeris의 palielināts, parastas lokalizācijas, asimetrisks 같은 텍스트가있는 경우 : LABA의 daiva의 lielāka을. Labà daiva 2,6x2,0 cm. Kreisā daiva 2,1x2,0 cm. 지협 0.4 cm. Kontūras nelīdzenas.Struktūra uzskatāmi rupjgraudaina, pazeminātu ehogenitāti, ar multipliem, zemas ehogenitātes ieslēgumiem. Mezgli nevizualizēas. ' 코드를 어떻게 변경해야합니까? @기음. Malette – vladimir

+0

이 문자열에서 무슨 문제가 있습니까? '
'이 아닌'\ r \ n'을 추가 하시겠습니까? – Sugar

+0

이 문자열에'\ r \ n' 또는'
'을 사용할 수 없습니다. 그렇지 않습니까? – vladimir

0

사용는 preg_match : $ 내림차순를 통해 루프에서 http://php.net/manual/en/function.preg-match.php

$string = 'THIS SENTENCE IS UPPERCASE'; 
$string2 = 'this sentece is in lowercase'; 

if(preg_match ('/[A-Z ]$/' , $string)) 
{ 
echo 'uppercase'; 
} 
else 
{ 
echo 'lowercase'; 
} 

사용이. $ string은 하나의 문자열을 포함하는 배열의 한 요소입니다.

/[A-Z] $ /는 모두 대문자로 공백을 찾습니다. 정규 표현식을 업그레이드하여 문자열에서 다른 것을 가져올 수 있습니다.

function split_upper_lower ($string) 
{ 
    $words = explode (' ', $string); 
    $i = 0; 
    foreach ($words as $word) 
    { 
     if (ctype_upper ($word)) 
      $new_string_array[++$i]['type'] = 'UPPER'; 
     else 
      $new_string_array[++$i]['type'] = 'LOWER'; 
     $new_string_array[$i]['word'] = $word; 
    } 
    $new_string = ''; 
    foreach ($new_string_array as $key => $new_word) 
    { 
     if (!isset ($current_mode)) 
     { 
      if (ctype_upper ($new_word)) 
       $current_mode = 'UPPER'; 
      else 
       $current_mode = 'LOWER'; 
     } 
     if ($new_word['type'] === $current_mode) 
     { 
      $new_string .= $new_word['word']; 
      if (isset ($new_string_array[$key + 1])) 
       $new_string .= ' '; 
     } 
     else 
     { 
      $new_string .= "\r\n" . $new_word['word']; 
      if (isset ($new_string_array[$key + 1])) 
       $new_string .= ' '; 
      if ($current_mode === 'UPPER') $current_mode = 'LOWER'; 
      else $current_mode = 'UPPER'; 
     } 
    } 
    return $new_string; 
} 

br와 함께 테스트 :

0

것은 당신이 할 수있는 이렇게 ...

$desc = array ("abcd","ABCD","bbb","B"); 

foreach($desc as $value) { 

    if(ctype_upper($value)) { 
     // character is upper 
    } else { 

     // character is lower 

    } 

} 
0

preg_match_all()을 사용하여 수행 할 수 있습니다. 다음 코드를 사용하여,

$result_pk_info = array(); 
    while ($row = odbc_fetch_array($sql_pk_info)) 
    { 
     $opisanie = iconv("cp1257", "UTF-8", trim($row['opis'])); 
     $id = iconv("cp1257", "UTF-8", trim($row['pacientid'])); 
     $date = iconv("cp1257", "UTF-8", trim($row['data'])); 

     $desc = explode('@#$', $opisanie); 
     //converting $desc array to string 
$string = implode(" " , $desc); 
//Upper case Matching 
$upprCase = preg_match_all('/[A-Z]/', $string, $uprmatches, PREG_OFFSET_CAPTURE); 

if($upprCase){ 
foreach ($uprmatches as $match) 
{ 
    foreach($match as $value) 
    //Iam a uppercase 
    print $UpperCase = $value[0]; 
}} 

//Splitting with \r\n 
print "\r\n"; 

//lower case matching 
$lowrCase = preg_match_all('/[a-z]/', $string, $lowrmatches, PREG_OFFSET_CAPTURE); 
if($lowrCase){ 
    foreach ($lowrmatches as $match) 
    { 
     foreach($match as $value) 
      //Iam a lowercase 
      print $lowerCase = $value[0]; 
    }} 
}