0

I have an array with many words, but some senteces are in uppercase. For example:

THIS SENTENCE IS UPPERCASE

this sentece is in lowercase

And I want to split this two sentences by \r\n, but I cant find how to do it

Here is how I retrive this array:

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

in $desc I have words array in which I want to search and split uppercase and lowercase.

So can anyone help me with it?

UPD the text which I have hase something like this structure: SENTENCE IN UPPERCASE Sentece in lower case

2

4 Answers 4

1

This function is what you're looking for :

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

Tested it with br :

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

Output :

HI 
how 
ARE 
you doing ?
Sign up to request clarification or add additional context in comments.

6 Comments

but if I have text like ]VAIROGDZIEDZERA <br> UN VIRSPUSĒJI <br> NOVIETOTO ORGĀNU US Vairogdziedzeris palielināts, parastas lokalizācijas, asimetrisks: labā daiva lielāka. Labā daiva 2,6x2,0 cm. Kreisā daiva 2,1x2,0 cm. Istmus 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ējas. how I need to change your code? @C. Malet
What's the problem happening with this string ? You want to add \r\n and not <br> right ?
it doesnot matter which \r\n or <br> I will use in this string, or it does?
It depends WHERE the new string will be used. In a file you'd want to use \r\n, for HTML display you'd want br
I display this string in html table
|
0

Use 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';
}

Use this in your loop through $desc. Where $string is one element of an array containing one string.

/[A-Z ]$/ will match all uppercase with spaces. You can just upgrade your regexp to grab something else from strings.

Comments

0

If I understood your question you can do like this ...

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

foreach($desc as $value) {

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

      // character is lower

    }

}

Comments

0

This can be done using preg_match_all(). Use the following code,

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.