PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Zend/Text/MultiByte.php

https://github.com/grandison/budo16
PHP | 153 lines | 86 code | 20 blank | 47 comment | 24 complexity | 01cbabb111aac58470b0a793d2768396 MD5 | raw file
  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-2009 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 16971 2009-07-22 18:05:45Z mikaelkael $
  20. */
  21. /**
  22. * Zend_Text_MultiByte contains multibyte safe string methods
  23. *
  24. * @category Zend
  25. * @package Zend_Text
  26. * @copyright Copyright (c) 2005-2009 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. $result = array();
  44. while (($stringLength = iconv_strlen($string, $charset)) > 0) {
  45. $subString = iconv_substr($string, 0, $width, $charset);
  46. if ($subString === $string) {
  47. $cutLength = null;
  48. } else {
  49. $nextChar = iconv_substr($string, $width, 1, $charset);
  50. if ($nextChar === ' ' || $nextChar === $break) {
  51. $afterNextChar = iconv_substr($string, $width + 1, 1, $charset);
  52. if ($afterNextChar === false) {
  53. $subString .= $nextChar;
  54. }
  55. $cutLength = iconv_strlen($subString, $charset) + 1;
  56. } else {
  57. $spacePos = iconv_strrpos($subString, ' ', $charset);
  58. if ($spacePos !== false) {
  59. $subString = iconv_substr($subString, 0, $spacePos, $charset);
  60. $cutLength = $spacePos + 1;
  61. } else if ($cut === false) {
  62. $spacePos = iconv_strpos($string, ' ', 0, $charset);
  63. if ($spacePos !== false) {
  64. $subString = iconv_substr($string, 0, $spacePos, $charset);
  65. $cutLength = $spacePos + 1;
  66. } else {
  67. $subString = $string;
  68. $cutLength = null;
  69. }
  70. } else {
  71. $breakPos = iconv_strpos($subString, $break, 0, $charset);
  72. if ($breakPos !== false) {
  73. $subString = iconv_substr($subString, 0, $breakPos, $charset);
  74. $cutLength = $breakPos + 1;
  75. } else {
  76. $subString = iconv_substr($subString, 0, $width, $charset);
  77. $cutLength = $width;
  78. }
  79. }
  80. }
  81. }
  82. $result[] = $subString;
  83. if ($cutLength !== null) {
  84. $string = iconv_substr($string, $cutLength, ($stringLength - $cutLength), $charset);
  85. } else {
  86. break;
  87. }
  88. }
  89. return implode($break, $result);
  90. }
  91. /**
  92. * String padding
  93. *
  94. * @param string $input
  95. * @param integer $padLength
  96. * @param string $padString
  97. * @param integer $padType
  98. * @param string $charset
  99. * @return string
  100. */
  101. public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'UTF-8')
  102. {
  103. $return = '';
  104. $lengthOfPadding = $padLength - iconv_strlen($input, $charset);
  105. $padStringLength = iconv_strlen($padString, $charset);
  106. if ($padStringLength === 0 || $lengthOfPadding === 0) {
  107. $return = $input;
  108. } else {
  109. $repeatCount = floor($lengthOfPadding / $padStringLength);
  110. if ($padType === STR_PAD_BOTH) {
  111. $lastStringLeft = '';
  112. $lastStringRight = '';
  113. $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;
  114. $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
  115. $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2);
  116. $lastStringRightLength += $lastStringLength % 2;
  117. $lastStringLeft = iconv_substr($padString, 0, $lastStringLeftLength, $charset);
  118. $lastStringRight = iconv_substr($padString, 0, $lastStringRightLength, $charset);
  119. $return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft
  120. . $input
  121. . str_repeat($padString, $repeatCountRight) . $lastStringRight;
  122. } else {
  123. $lastString = iconv_substr($padString, 0, $lengthOfPadding % $padStringLength, $charset);
  124. if ($padType === STR_PAD_LEFT) {
  125. $return = str_repeat($padString, $repeatCount) . $lastString . $input;
  126. } else {
  127. $return = $input . str_repeat($padString, $repeatCount) . $lastString;
  128. }
  129. }
  130. }
  131. return $return;
  132. }
  133. }