MultiByte.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Zend_Text_MultiByte contains multibyte safe string methods
  23. *
  24. * @category Zend
  25. * @package Zend_Text
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Text_MultiByte
  30. {
  31. /**
  32. * Word wrap
  33. *
  34. * @param string $string
  35. * @param integer $width
  36. * @param string $break
  37. * @param boolean $cut
  38. * @param string $charset
  39. * @return string
  40. */
  41. public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'utf-8')
  42. {
  43. $stringWidth = iconv_strlen($string, $charset);
  44. $breakWidth = iconv_strlen($break, $charset);
  45. if (strlen($string) === 0) {
  46. return '';
  47. } elseif ($breakWidth === null) {
  48. throw new Zend_Text_Exception('Break string cannot be empty');
  49. } elseif ($width === 0 && $cut) {
  50. throw new Zend_Text_Exception('Can\'t force cut when width is zero');
  51. }
  52. $result = '';
  53. $lastStart = $lastSpace = 0;
  54. for ($current = 0; $current < $stringWidth; $current++) {
  55. $char = iconv_substr($string, $current, 1, $charset);
  56. if ($breakWidth === 1) {
  57. $possibleBreak = $char;
  58. } else {
  59. $possibleBreak = iconv_substr($string, $current, $breakWidth, $charset);
  60. }
  61. if ($possibleBreak === $break) {
  62. $result .= iconv_substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset);
  63. $current += $breakWidth - 1;
  64. $lastStart = $lastSpace = $current + 1;
  65. } elseif ($char === ' ') {
  66. if ($current - $lastStart >= $width) {
  67. $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break;
  68. $lastStart = $current + 1;
  69. }
  70. $lastSpace = $current;
  71. } elseif ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) {
  72. $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break;
  73. $lastStart = $lastSpace = $current;
  74. } elseif ($current - $lastStart >= $width && $lastStart < $lastSpace) {
  75. $result .= iconv_substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break;
  76. $lastStart = $lastSpace = $lastSpace + 1;
  77. }
  78. }
  79. if ($lastStart !== $current) {
  80. $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset);
  81. }
  82. return $result;
  83. }
  84. /**
  85. * String padding
  86. *
  87. * @param string $input
  88. * @param integer $padLength
  89. * @param string $padString
  90. * @param integer $padType
  91. * @param string $charset
  92. * @return string
  93. */
  94. public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'utf-8')
  95. {
  96. $return = '';
  97. $lengthOfPadding = $padLength - iconv_strlen($input, $charset);
  98. $padStringLength = iconv_strlen($padString, $charset);
  99. if ($padStringLength === 0 || $lengthOfPadding <= 0) {
  100. $return = $input;
  101. } else {
  102. $repeatCount = floor($lengthOfPadding / $padStringLength);
  103. if ($padType === STR_PAD_BOTH) {
  104. $lastStringLeft = '';
  105. $lastStringRight = '';
  106. $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;
  107. $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
  108. $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2);
  109. $lastStringRightLength += $lastStringLength % 2;
  110. $lastStringLeft = iconv_substr($padString, 0, $lastStringLeftLength, $charset);
  111. $lastStringRight = iconv_substr($padString, 0, $lastStringRightLength, $charset);
  112. $return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft
  113. . $input
  114. . str_repeat($padString, $repeatCountRight) . $lastStringRight;
  115. } else {
  116. $lastString = iconv_substr($padString, 0, $lengthOfPadding % $padStringLength, $charset);
  117. if ($padType === STR_PAD_LEFT) {
  118. $return = str_repeat($padString, $repeatCount) . $lastString . $input;
  119. } else {
  120. $return = $input . str_repeat($padString, $repeatCount) . $lastString;
  121. }
  122. }
  123. }
  124. return $return;
  125. }
  126. }