app/Module/FanChartModule.php
protected function splitAlignText(string $data, int $maxlen): string
    {
        $RTLOrd = [
            215,
            216,
            217,
            218,
            219,
        ];

        $lines = explode("\n", $data);
        // more than 1 line : recursive calls
        if (count($lines) > 1) {
            $text = '';
            foreach ($lines as $line) {
                $text .= $this->splitAlignText($line, $maxlen) . "\n";
            }

            return $text;
        }
        // process current line word by word
        $split = explode(' ', $data);
        $text  = '';
        $line  = '';

        // do not split hebrew line
        $found = false;
        foreach ($RTLOrd as $ord) {
            if (strpos($data, chr($ord)) !== false) {
                $found = true;
            }
        }
        if ($found) {
            $line = $data;
        } else {
            foreach ($split as $word) {
                $len  = strlen($line);
                $wlen = strlen($word);
                if (($len + $wlen) < $maxlen) {
                    if (!empty($line)) {
                        $line .= ' ';
                    }
                    $line .= $word;
                } else {
                    $p = max(0, (int) (($maxlen - $len) / 2));
                    if (!empty($line)) {
                        $line = str_repeat(' ', $p) . $line; // center alignment using spaces
                        $text .= $line . "\n";
                    }
                    $line = $word;
                }
            }
        }
        // last line
        if (!empty($line)) {
            $len = strlen($line);
            if (in_array(ord($line{0}), $RTLOrd, true)) {
                $len /= 2;
            }
            $p    = max(0, (int) (($maxlen - $len) / 2));
            $line = str_repeat(' ', $p) . $line; // center alignment using spaces
            $text .= $line;
        }

        return $text;
    }
protected function splitAlignText(string $data, int $maxlen): string
    {
        $RTLOrd = [
            215,
            216,
            217,
            218,
            219,
        ];

        $lines = explode("\n", $data);
        // more than 1 line : recursive calls
        if (count($lines) > 1) {
            $text = '';
            foreach ($lines as $line) {
                $text .= $this->splitAlignText($line, $maxlen) . "\n";
            }

            return $text;
        }
        // process current line word by word
        $split = explode(' ', $data);
        $text  = '';
        $line  = '';

        // do not split hebrew line
        $found = false;
        foreach ($RTLOrd as $ord) {
            if (strpos($data, chr($ord)) !== false) {
                $found = true;
            }
        }
        if ($found) {
            $line = $data;
        } else {
            foreach ($split as $word) {
                $len  = strlen($line);
                $wlen = strlen($word);
                if (($len + $wlen) < $maxlen) {
                    if (!empty($line)) {
                        $line .= ' ';
                    }
                    $line .= $word;
                } else {
                    $p = max(0, (int) (($maxlen - $len) / 2));
                    if (!empty($line)) {
                        $line = str_repeat(' ', $p) . $line; // center alignment using spaces
                        $text .= $line . "\n";
                    }
                    $line = $word;
                }
            }
        }
        // last line
        if (!empty($line)) {
            $len = strlen($line);
            if (in_array(ord($line{0}), $RTLOrd, true)) {
                $len /= 2;
            }
            $p    = max(0, (int) (($maxlen - $len) / 2));
            $line = str_repeat(' ', $p) . $line; // center alignment using spaces
            $text .= $line;
        }

        return $text;
    }
protected function splitAlignText ( string $ data , int $ maxlen ) : string { $ RTLOrd = [ 215 , 216 , 217 , 218 , 219 , ] ; $ lines = explode ( "\n" , $ data ) ; // more than 1 line : recursive calls if ( count ( $ lines ) > 1 ) { $ text = '' ; foreach ( $ lines as $ line ) { $ text .= $ this -> splitAlignText ( $ line , $ maxlen ) . "\n" ; } return $ text ; } // process current line word by word $ split = explode ( ' ' , $ data ) ; $ text = '' ; $ line = '' ; // do not split hebrew line $ found = false ; foreach ( $ RTLOrd as $ ord ) { if ( strpos ( $ data , chr ( $ ord ) ) !== false ) { $ found = true ; } } if ( $ found ) { $ line = $ data ; } else { foreach ( $ split as $ word ) { $ len = strlen ( $ line ) ; $ wlen = strlen ( $ word ) ; if ( ( $ len + $ wlen ) < $ maxlen ) { if ( ! empty ( $ line ) ) { $ line .= ' ' ; } $ line .= $ word ; } else { $ p = max ( 0 , ( int ) ( ( $ maxlen - $ len ) / 2 ) ) ; if ( ! empty ( $ line ) ) { $ line = str_repeat ( ' ' , $ p ) . $ line ; // center alignment using spaces $ text .= $ line . "\n" ; } $ line = $ word ; } } } // last line if ( ! empty ( $ line ) ) { $ len = strlen ( $ line ) ; if ( in_array ( ord ( $ line { 0 } ) , $ RTLOrd , true ) ) { $ len /= 2 ; } $ p = max ( 0 , ( int ) ( ( $ maxlen - $ len ) / 2 ) ) ; $ line = str_repeat ( ' ' , $ p ) . $ line ; // center alignment using spaces $ text .= $ line ; } return $ text ; }
split and center text by lines

@param string $data   input string
@param int    $maxlen max length of each line

@return string $text output string
split and center text by lines