|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Text |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: MultiByte.php 21931 2010-04-18 15:25:32Z dasprid $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * Zend_Text_MultiByte contains multibyte safe string methods |
|
24 * |
|
25 * @category Zend |
|
26 * @package Zend_Text |
|
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
29 */ |
|
30 class Zend_Text_MultiByte |
|
31 { |
|
32 /** |
|
33 * Word wrap |
|
34 * |
|
35 * @param string $string |
|
36 * @param integer $width |
|
37 * @param string $break |
|
38 * @param boolean $cut |
|
39 * @param string $charset |
|
40 * @return string |
|
41 */ |
|
42 public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'UTF-8') |
|
43 { |
|
44 $result = array(); |
|
45 $breakWidth = iconv_strlen($break, $charset); |
|
46 |
|
47 while (($stringLength = iconv_strlen($string, $charset)) > 0) { |
|
48 $breakPos = iconv_strpos($string, $break, 0, $charset); |
|
49 |
|
50 if ($breakPos !== false && $breakPos < $width) { |
|
51 if ($breakPos === $stringLength - $breakWidth) { |
|
52 $subString = $string; |
|
53 $cutLength = null; |
|
54 } else { |
|
55 $subString = iconv_substr($string, 0, $breakPos, $charset); |
|
56 $cutLength = $breakPos + $breakWidth; |
|
57 } |
|
58 } else { |
|
59 $subString = iconv_substr($string, 0, $width, $charset); |
|
60 |
|
61 if ($subString === $string) { |
|
62 $cutLength = null; |
|
63 } else { |
|
64 $nextChar = iconv_substr($string, $width, 1, $charset); |
|
65 |
|
66 if ($breakWidth === 1) { |
|
67 $nextBreak = $nextChar; |
|
68 } else { |
|
69 $nextBreak = iconv_substr($string, $breakWidth, 1, $charset); |
|
70 } |
|
71 |
|
72 if ($nextChar === ' ' || $nextBreak === $break) { |
|
73 $afterNextChar = iconv_substr($string, $width + 1, 1, $charset); |
|
74 |
|
75 if ($afterNextChar === false) { |
|
76 $subString .= $nextChar; |
|
77 } |
|
78 |
|
79 $cutLength = iconv_strlen($subString, $charset) + 1; |
|
80 } else { |
|
81 $spacePos = iconv_strrpos($subString, ' ', $charset); |
|
82 |
|
83 if ($spacePos !== false) { |
|
84 $subString = iconv_substr($subString, 0, $spacePos, $charset); |
|
85 $cutLength = $spacePos + 1; |
|
86 } else if ($cut === false) { |
|
87 $spacePos = iconv_strpos($string, ' ', 0, $charset); |
|
88 |
|
89 if ($spacePos !== false) { |
|
90 $subString = iconv_substr($string, 0, $spacePos, $charset); |
|
91 $cutLength = $spacePos + 1; |
|
92 } else { |
|
93 $subString = $string; |
|
94 $cutLength = null; |
|
95 } |
|
96 } else { |
|
97 $subString = iconv_substr($subString, 0, $width, $charset); |
|
98 $cutLength = $width; |
|
99 } |
|
100 } |
|
101 } |
|
102 } |
|
103 |
|
104 $result[] = $subString; |
|
105 |
|
106 if ($cutLength !== null) { |
|
107 $string = iconv_substr($string, $cutLength, ($stringLength - $cutLength), $charset); |
|
108 } else { |
|
109 break; |
|
110 } |
|
111 } |
|
112 |
|
113 return implode($break, $result); |
|
114 } |
|
115 |
|
116 /** |
|
117 * String padding |
|
118 * |
|
119 * @param string $input |
|
120 * @param integer $padLength |
|
121 * @param string $padString |
|
122 * @param integer $padType |
|
123 * @param string $charset |
|
124 * @return string |
|
125 */ |
|
126 public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'UTF-8') |
|
127 { |
|
128 $return = ''; |
|
129 $lengthOfPadding = $padLength - iconv_strlen($input, $charset); |
|
130 $padStringLength = iconv_strlen($padString, $charset); |
|
131 |
|
132 if ($padStringLength === 0 || $lengthOfPadding === 0) { |
|
133 $return = $input; |
|
134 } else { |
|
135 $repeatCount = floor($lengthOfPadding / $padStringLength); |
|
136 |
|
137 if ($padType === STR_PAD_BOTH) { |
|
138 $lastStringLeft = ''; |
|
139 $lastStringRight = ''; |
|
140 $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2; |
|
141 |
|
142 $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength; |
|
143 $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2); |
|
144 $lastStringRightLength += $lastStringLength % 2; |
|
145 |
|
146 $lastStringLeft = iconv_substr($padString, 0, $lastStringLeftLength, $charset); |
|
147 $lastStringRight = iconv_substr($padString, 0, $lastStringRightLength, $charset); |
|
148 |
|
149 $return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft |
|
150 . $input |
|
151 . str_repeat($padString, $repeatCountRight) . $lastStringRight; |
|
152 } else { |
|
153 $lastString = iconv_substr($padString, 0, $lengthOfPadding % $padStringLength, $charset); |
|
154 |
|
155 if ($padType === STR_PAD_LEFT) { |
|
156 $return = str_repeat($padString, $repeatCount) . $lastString . $input; |
|
157 } else { |
|
158 $return = $input . str_repeat($padString, $repeatCount) . $lastString; |
|
159 } |
|
160 } |
|
161 } |
|
162 |
|
163 return $return; |
|
164 } |
|
165 } |