File indexing completed on 2024-12-22 05:37:08
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Text 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * Zend_Text_MultiByte contains multibyte safe string methods 0024 * 0025 * @category Zend 0026 * @package Zend_Text 0027 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0028 * @license http://framework.zend.com/license/new-bsd New BSD License 0029 */ 0030 class Zend_Text_MultiByte 0031 { 0032 /** 0033 * Word wrap 0034 * 0035 * @param string $string 0036 * @param integer $width 0037 * @param string $break 0038 * @param boolean $cut 0039 * @param string $charset 0040 * @return string 0041 */ 0042 public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'utf-8') 0043 { 0044 $stringWidth = iconv_strlen($string, $charset); 0045 $breakWidth = iconv_strlen($break, $charset); 0046 0047 if (strlen($string) === 0) { 0048 return ''; 0049 } elseif ($breakWidth === null) { 0050 throw new Zend_Text_Exception('Break string cannot be empty'); 0051 } elseif ($width === 0 && $cut) { 0052 throw new Zend_Text_Exception('Can\'t force cut when width is zero'); 0053 } 0054 0055 $result = ''; 0056 $lastStart = $lastSpace = 0; 0057 0058 for ($current = 0; $current < $stringWidth; $current++) { 0059 $char = iconv_substr($string, $current, 1, $charset); 0060 0061 if ($breakWidth === 1) { 0062 $possibleBreak = $char; 0063 } else { 0064 $possibleBreak = iconv_substr($string, $current, $breakWidth, $charset); 0065 } 0066 0067 if ($possibleBreak === $break) { 0068 $result .= iconv_substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset); 0069 $current += $breakWidth - 1; 0070 $lastStart = $lastSpace = $current + 1; 0071 } elseif ($char === ' ') { 0072 if ($current - $lastStart >= $width) { 0073 $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; 0074 $lastStart = $current + 1; 0075 } 0076 0077 $lastSpace = $current; 0078 } elseif ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { 0079 $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; 0080 $lastStart = $lastSpace = $current; 0081 } elseif ($current - $lastStart >= $width && $lastStart < $lastSpace) { 0082 $result .= iconv_substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break; 0083 $lastStart = $lastSpace = $lastSpace + 1; 0084 } 0085 } 0086 0087 if ($lastStart !== $current) { 0088 $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset); 0089 } 0090 0091 return $result; 0092 } 0093 0094 /** 0095 * String padding 0096 * 0097 * @param string $input 0098 * @param integer $padLength 0099 * @param string $padString 0100 * @param integer $padType 0101 * @param string $charset 0102 * @return string 0103 */ 0104 public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'utf-8') 0105 { 0106 $return = ''; 0107 $lengthOfPadding = $padLength - iconv_strlen($input, $charset); 0108 $padStringLength = iconv_strlen($padString, $charset); 0109 0110 if ($padStringLength === 0 || $lengthOfPadding <= 0) { 0111 $return = $input; 0112 } else { 0113 $repeatCount = floor($lengthOfPadding / $padStringLength); 0114 0115 if ($padType === STR_PAD_BOTH) { 0116 $lastStringLeft = ''; 0117 $lastStringRight = ''; 0118 $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2; 0119 0120 $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength; 0121 $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2); 0122 $lastStringRightLength += $lastStringLength % 2; 0123 0124 $lastStringLeft = iconv_substr($padString, 0, $lastStringLeftLength, $charset); 0125 $lastStringRight = iconv_substr($padString, 0, $lastStringRightLength, $charset); 0126 0127 $return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft 0128 . $input 0129 . str_repeat($padString, $repeatCountRight) . $lastStringRight; 0130 } else { 0131 $lastString = iconv_substr($padString, 0, $lengthOfPadding % $padStringLength, $charset); 0132 0133 if ($padType === STR_PAD_LEFT) { 0134 $return = str_repeat($padString, $repeatCount) . $lastString . $input; 0135 } else { 0136 $return = $input . str_repeat($padString, $repeatCount) . $lastString; 0137 } 0138 } 0139 } 0140 0141 return $return; 0142 } 0143 }