src/Utilities/Miscellaneous.php
public static function breakLongText(
        $text,
        $perLine = 100,
        $separator = '<br>',
        $encoding = 'utf-8',
        $proportionalAberration = 20
    ) {
        $effect = $text;
        $textLength = mb_strlen($text);

        if (!empty($text) && $textLength > $perLine) {
            /*
             * The html_entity_decode() function is used here, because while operating
             * on string that contains only special characters the string is divided
             * incorrectly, e.g. "<<<<<" -> "&lt;&lt;&lt;&lt;&<br />lt;".
             */
            //$text = htmlspecialchars_decode($text);
            $text = html_entity_decode($text, ENT_QUOTES);

            $effect = '';
            $currentPosition = 0;

            $charsAberration = ceil($perLine * ($proportionalAberration / 100));
            $charsPerLineDefault = $perLine;

            while ($currentPosition <= $textLength) {
                $insertSeparator = false;

                /*
                 * Looking for spaces before and after current position. It was done, because text wasn't
                 * broken properly and some words were breaked and placed into two lines.
                 */
                if ($charsAberration > 0) {
                    $length = $perLine + $charsAberration;
                    $lineWithAberration = mb_substr($text, $currentPosition, $length, $encoding);

                    if (!Regex::contains($lineWithAberration, ' ')) {
                        $length = $perLine - $charsAberration;
                        $lineWithAberration = mb_substr($text, $currentPosition, $length, $encoding);
                    }

                    if (Regex::startsWith($lineWithAberration, ' ')) {
                        ++$currentPosition;
                        $lineWithAberration = ltrim($lineWithAberration);
                    }

                    $spacePosition = mb_strrpos($lineWithAberration, ' ', 0, $encoding);

                    if (false !== $spacePosition && 0 < $spacePosition) {
                        /** @var int $spacePosition */
                        $perLine = $spacePosition;
                        $insertSeparator = true;
                    }
                }

                $charsOneLine = mb_substr($text, $currentPosition, $perLine, $encoding);

                /*
                 * The htmlspecialchars() function is used here, because...
                 * Reason and comment the same as above for html_entity_decode() function.
                 */

                $effect .= htmlspecialchars($charsOneLine);
                //$effect .= $charsOneLine;

                $currentPosition += $perLine;
                $oneLineContainsSpace = Regex::contains($charsOneLine, ' ');

                if (($insertSeparator || !$oneLineContainsSpace) && $currentPosition <= $textLength) {
                    $effect .= $separator;
                }

                $perLine = $charsPerLineDefault;
            }
        }

        return $effect;
    }
public static function breakLongText(
        $text,
        $perLine = 100,
        $separator = '<br>',
        $encoding = 'utf-8',
        $proportionalAberration = 20
    ) {
        $effect = $text;
        $textLength = mb_strlen($text);

        if (!empty($text) && $textLength > $perLine) {
            /*
             * The html_entity_decode() function is used here, because while operating
             * on string that contains only special characters the string is divided
             * incorrectly, e.g. "<<<<<" -> "&lt;&lt;&lt;&lt;&<br />lt;".
             */
            //$text = htmlspecialchars_decode($text);
            $text = html_entity_decode($text, ENT_QUOTES);

            $effect = '';
            $currentPosition = 0;

            $charsAberration = ceil($perLine * ($proportionalAberration / 100));
            $charsPerLineDefault = $perLine;

            while ($currentPosition <= $textLength) {
                $insertSeparator = false;

                /*
                 * Looking for spaces before and after current position. It was done, because text wasn't
                 * broken properly and some words were breaked and placed into two lines.
                 */
                if ($charsAberration > 0) {
                    $length = $perLine + $charsAberration;
                    $lineWithAberration = mb_substr($text, $currentPosition, $length, $encoding);

                    if (!Regex::contains($lineWithAberration, ' ')) {
                        $length = $perLine - $charsAberration;
                        $lineWithAberration = mb_substr($text, $currentPosition, $length, $encoding);
                    }

                    if (Regex::startsWith($lineWithAberration, ' ')) {
                        ++$currentPosition;
                        $lineWithAberration = ltrim($lineWithAberration);
                    }

                    $spacePosition = mb_strrpos($lineWithAberration, ' ', 0, $encoding);

                    if (false !== $spacePosition && 0 < $spacePosition) {
                        /** @var int $spacePosition */
                        $perLine = $spacePosition;
                        $insertSeparator = true;
                    }
                }

                $charsOneLine = mb_substr($text, $currentPosition, $perLine, $encoding);

                /*
                 * The htmlspecialchars() function is used here, because...
                 * Reason and comment the same as above for html_entity_decode() function.
                 */

                $effect .= htmlspecialchars($charsOneLine);
                //$effect .= $charsOneLine;

                $currentPosition += $perLine;
                $oneLineContainsSpace = Regex::contains($charsOneLine, ' ');

                if (($insertSeparator || !$oneLineContainsSpace) && $currentPosition <= $textLength) {
                    $effect .= $separator;
                }

                $perLine = $charsPerLineDefault;
            }
        }

        return $effect;
    }
public static function breakLongText ( $ text , $ perLine = 100 , $ separator = '<br>' , $ encoding = 'utf-8' , $ proportionalAberration = 20 ) { $ effect = $ text ; $ textLength = mb_strlen ( $ text ) ; if ( ! empty ( $ text ) && $ textLength > $ perLine ) { /*
             * The html_entity_decode() function is used here, because while operating
             * on string that contains only special characters the string is divided
             * incorrectly, e.g. "<<<<<" -> "&lt;&lt;&lt;&lt;&<br />lt;".
             */ //$text = htmlspecialchars_decode($text); $ text = html_entity_decode ( $ text , ENT_QUOTES ) ; $ effect = '' ; $ currentPosition = 0 ; $ charsAberration = ceil ( $ perLine * ( $ proportionalAberration / 100 ) ) ; $ charsPerLineDefault = $ perLine ; while ( $ currentPosition <= $ textLength ) { $ insertSeparator = false ; /*
                 * Looking for spaces before and after current position. It was done, because text wasn't
                 * broken properly and some words were breaked and placed into two lines.
                 */ if ( $ charsAberration > 0 ) { $ length = $ perLine + $ charsAberration ; $ lineWithAberration = mb_substr ( $ text , $ currentPosition , $ length , $ encoding ) ; if ( ! Regex :: contains ( $ lineWithAberration , ' ' ) ) { $ length = $ perLine - $ charsAberration ; $ lineWithAberration = mb_substr ( $ text , $ currentPosition , $ length , $ encoding ) ; } if ( Regex :: startsWith ( $ lineWithAberration , ' ' ) ) { ++ $ currentPosition ; $ lineWithAberration = ltrim ( $ lineWithAberration ) ; } $ spacePosition = mb_strrpos ( $ lineWithAberration , ' ' , 0 , $ encoding ) ; if ( false !== $ spacePosition && 0 < $ spacePosition ) { /** @var int $spacePosition */ $ perLine = $ spacePosition ; $ insertSeparator = true ; } } $ charsOneLine = mb_substr ( $ text , $ currentPosition , $ perLine , $ encoding ) ; /*
                 * The htmlspecialchars() function is used here, because...
                 * Reason and comment the same as above for html_entity_decode() function.
                 */ $ effect .= htmlspecialchars ( $ charsOneLine ) ; //$effect .= $charsOneLine; $ currentPosition += $ perLine ; $ oneLineContainsSpace = Regex :: contains ( $ charsOneLine , ' ' ) ; if ( ( $ insertSeparator || ! $ oneLineContainsSpace ) && $ currentPosition <= $ textLength ) { $ effect .= $ separator ; } $ perLine = $ charsPerLineDefault ; } } return $ effect ; }
Breaks long text

@param string $text                   The text to check and break
@param int    $perLine                (optional) Characters count per line. Default: 100.
@param string $separator              (optional) Separator that is placed between lines. Default: "<br>".
@param string $encoding               (optional) Character encoding. Used by mb_substr(). Default: "UTF-8".
@param int    $proportionalAberration (optional) Proportional aberration for chars (percent value). Default: 20.
@return string
Breaks long text