Question

I'm trying to take a multibyte string of Greek characters and trim it down to just 3 lines. My approach has been to split the multibyte string into a character array and then loop through it and try to detect line breaks within the string. However, the string isn't being trimmed at all and giving me some strange errors.

Here's the code:

$break_count = 0;
            $char_array = array();
            $speech_string = ""; 
            $break_array = array();
            foreach($total_found as $speech)
            {   
                    $temp = preg_split('/(?<!^)(?!$)/u', $speech->speech_text );  
                    foreach($temp as $char){
                            if($char=="\n"){
                                    $break_count++;
                                    array_push($break_array,"LB");
                            }   
                            if($break_count < 4)
                            {   
                                    array_push($char_array,$char);
                            }else{  
                                    die(print_r(var_export($break_array) . " : " . $break_count,1));
                                    break;
                            }                               
                    }   
                    $string = join("",$char_array);
                    array_push($speech_array,$string);
                    $break_count = 0;
                    $loop_count++;
            }

$break_count is the variable I'm using to detect the number of line breaks encountered so far and when there are more than 3, characters should stop being pushed to the $char_array variable. The print_r should return 4 line breaks and an array containing 4 "LB" elements. However, it returns the following: array ( 0 => 'LB', 1 => 'LB', 2 => 'LB', 3 => 'LB', 4 => 'LB', 5 => 'LB', 6 => 'LB', 7 => 'LB', 8 => 'LB', ) : 4 which means that my code is pushing more line breaks onto the $break_array even though it should break out of the 4 loop after $break_count = 4.

Any help is much appreciated.

Was it helpful?

Solution

Does this do what you want?

$allFirst3Lines = array();
foreach($total_found as $speech)
{
    $first3LinesArray = array_slice(explode("\n", $speech->speech_text), 0, 3);
    array_push($allFirst3Lines, implode("\n", $first3LinesArray));
}

OTHER TIPS

If your multibyte string is saved as UTF-8, then line-breaks are binary compatible with ASCII line-breaks. Simply splitting by regular line-breaks is perfectly safe, even with the non-multibyte-aware standard functions.

$string = "これ\nは\nユーティーエッフエイト\nだぞ!";
echo join("\n", array_slice(explode("\n", $string), 0, 3));

> これ
> は
> ユーティーエッフエイト
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top