12 * obtain it through the world-wide-web, please send an email |
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. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Text |
16 * @package Zend_Text |
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
17 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
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 $ |
19 * @version $Id: MultiByte.php 24762 2012-05-06 00:06:46Z adamlundrigan $ |
20 */ |
20 */ |
21 |
21 |
22 /** |
22 /** |
23 * Zend_Text_MultiByte contains multibyte safe string methods |
23 * Zend_Text_MultiByte contains multibyte safe string methods |
24 * |
24 * |
25 * @category Zend |
25 * @category Zend |
26 * @package Zend_Text |
26 * @package Zend_Text |
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
27 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
29 */ |
29 */ |
30 class Zend_Text_MultiByte |
30 class Zend_Text_MultiByte |
31 { |
31 { |
32 /** |
32 /** |
37 * @param string $break |
37 * @param string $break |
38 * @param boolean $cut |
38 * @param boolean $cut |
39 * @param string $charset |
39 * @param string $charset |
40 * @return string |
40 * @return string |
41 */ |
41 */ |
42 public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'UTF-8') |
42 public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'utf-8') |
43 { |
43 { |
44 $result = array(); |
44 $stringWidth = iconv_strlen($string, $charset); |
45 $breakWidth = iconv_strlen($break, $charset); |
45 $breakWidth = iconv_strlen($break, $charset); |
46 |
46 |
47 while (($stringLength = iconv_strlen($string, $charset)) > 0) { |
47 if (strlen($string) === 0) { |
48 $breakPos = iconv_strpos($string, $break, 0, $charset); |
48 return ''; |
49 |
49 } elseif ($breakWidth === null) { |
50 if ($breakPos !== false && $breakPos < $width) { |
50 throw new Zend_Text_Exception('Break string cannot be empty'); |
51 if ($breakPos === $stringLength - $breakWidth) { |
51 } elseif ($width === 0 && $cut) { |
52 $subString = $string; |
52 throw new Zend_Text_Exception('Can\'t force cut when width is zero'); |
53 $cutLength = null; |
53 } |
54 } else { |
54 |
55 $subString = iconv_substr($string, 0, $breakPos, $charset); |
55 $result = ''; |
56 $cutLength = $breakPos + $breakWidth; |
56 $lastStart = $lastSpace = 0; |
|
57 |
|
58 for ($current = 0; $current < $stringWidth; $current++) { |
|
59 $char = iconv_substr($string, $current, 1, $charset); |
|
60 |
|
61 if ($breakWidth === 1) { |
|
62 $possibleBreak = $char; |
|
63 } else { |
|
64 $possibleBreak = iconv_substr($string, $current, $breakWidth, $charset); |
|
65 } |
|
66 |
|
67 if ($possibleBreak === $break) { |
|
68 $result .= iconv_substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset); |
|
69 $current += $breakWidth - 1; |
|
70 $lastStart = $lastSpace = $current + 1; |
|
71 } elseif ($char === ' ') { |
|
72 if ($current - $lastStart >= $width) { |
|
73 $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; |
|
74 $lastStart = $current + 1; |
57 } |
75 } |
58 } else { |
76 |
59 $subString = iconv_substr($string, 0, $width, $charset); |
77 $lastSpace = $current; |
60 |
78 } elseif ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { |
61 if ($subString === $string) { |
79 $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; |
62 $cutLength = null; |
80 $lastStart = $lastSpace = $current; |
63 } else { |
81 } elseif ($current - $lastStart >= $width && $lastStart < $lastSpace) { |
64 $nextChar = iconv_substr($string, $width, 1, $charset); |
82 $result .= iconv_substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break; |
65 |
83 $lastStart = $lastSpace = $lastSpace + 1; |
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 } |
84 } |
111 } |
85 } |
112 |
86 |
113 return implode($break, $result); |
87 if ($lastStart !== $current) { |
|
88 $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset); |
|
89 } |
|
90 |
|
91 return $result; |
114 } |
92 } |
115 |
93 |
116 /** |
94 /** |
117 * String padding |
95 * String padding |
118 * |
96 * |
121 * @param string $padString |
99 * @param string $padString |
122 * @param integer $padType |
100 * @param integer $padType |
123 * @param string $charset |
101 * @param string $charset |
124 * @return string |
102 * @return string |
125 */ |
103 */ |
126 public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'UTF-8') |
104 public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'utf-8') |
127 { |
105 { |
128 $return = ''; |
106 $return = ''; |
129 $lengthOfPadding = $padLength - iconv_strlen($input, $charset); |
107 $lengthOfPadding = $padLength - iconv_strlen($input, $charset); |
130 $padStringLength = iconv_strlen($padString, $charset); |
108 $padStringLength = iconv_strlen($padString, $charset); |
131 |
109 |
132 if ($padStringLength === 0 || $lengthOfPadding === 0) { |
110 if ($padStringLength === 0 || $lengthOfPadding <= 0) { |
133 $return = $input; |
111 $return = $input; |
134 } else { |
112 } else { |
135 $repeatCount = floor($lengthOfPadding / $padStringLength); |
113 $repeatCount = floor($lengthOfPadding / $padStringLength); |
136 |
114 |
137 if ($padType === STR_PAD_BOTH) { |
115 if ($padType === STR_PAD_BOTH) { |