String.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace KIF\String;
  3. use KIF\Data\Convert;
  4. class String {
  5. static public function stripslashes($string) {
  6. if (is_array($string)) {
  7. foreach ($string as $key => $val) {
  8. $string[$key] = self::stripslashes($val);
  9. }
  10. } else {
  11. $string = \stripslashes($string);
  12. }
  13. return $string;
  14. }
  15. /**
  16. * 拼接url和参数,构造新的url
  17. * @param string $url 旧的url
  18. * @param array $args 需附加到url上的请求参数
  19. * @return string
  20. */
  21. static public function jointUrl($url, array $args) {
  22. $new_url = $url . ((strpos($url, '?') === false) ? '?' : '&') . http_build_query($args);
  23. return $new_url;
  24. }
  25. /**
  26. * 以给定显示长度截取字符串,如果执行了截取操作,那么在最后加上$dot
  27. * utf-8 下的字宽 一般来说 一个中文是2 一个英文是 1
  28. * @author Gxg <gaoxiaogang@Gmail.com>
  29. * @param String $text
  30. * @param Int $length
  31. * @param string $dot
  32. * @return String
  33. */
  34. static public function suitLength($text, $length, $middle = false, $dot = '...', $encoding = 'UTF-8') {
  35. $iLength = mb_strwidth($text, $encoding);
  36. if ($iLength < $length) { //要求的宽度咋能超过字符串总宽呢,原样返回
  37. return $text;
  38. }
  39. if ($middle) {
  40. if (floor($length / 2) < $length / 2) {
  41. $iLeftLength = floor($length / 2);
  42. $iRightLength = $iLeftLength + 1;
  43. } else {
  44. $iLeftLength = floor($length / 2);
  45. $iRightLength = $iLeftLength;
  46. }
  47. return mb_strimwidth($text, 0, $iLeftLength, '', $encoding) . $dot . self::yoka_mb_strimwidth($text, mb_strlen($text, $encoding), -$iRightLength, '', $encoding);
  48. } else {
  49. return mb_strimwidth($text, 0, $length, $dot, $encoding);
  50. }
  51. }
  52. /**
  53. * 增强型mb_strimwidth()函数,如果 $width 为负,支持向前截取字符
  54. *
  55. * @param String $str
  56. * @param Int $start
  57. * @param Int $width
  58. * @param String $trimmarker
  59. * @param String $encoding
  60. * @return String
  61. */
  62. static public function yoka_mb_strimwidth($str, $start, $width,$trimmarker='',$encoding=null) {
  63. if(!isset($encoding)) {
  64. $encoding = mb_internal_encoding();
  65. }
  66. if(0 > $width) {//向前截取
  67. $strBack = mb_substr($str,$start,mb_strlen($str,$encoding),$encoding);
  68. $iBackWidth = mb_strwidth($strBack,$encoding);
  69. $strFore = mb_substr($str,0,mb_strlen($str,$encoding)-mb_strlen($strBack,$encoding),$encoding);
  70. $iForeWidth = mb_strwidth($strFore,$encoding);
  71. if($iForeWidth <= -$width) {
  72. return $strFore;
  73. }
  74. $iForeWidth_1 = $iForeWidth + $width;
  75. $strFore_1 = mb_strimwidth($strFore,0,$iForeWidth_1,'',$encoding);
  76. $strFore_2 = mb_substr($strFore, mb_strlen($strFore_1,$encoding), $iForeWidth, $encoding);
  77. return $strFore_2;
  78. } else {
  79. return mb_strimwidth($str, $start, $width, $trimmarker, $encoding);
  80. }
  81. }
  82. /**
  83. * 去除 bbcode 标签 转换为纯文本
  84. *
  85. * ###################
  86. * ## Modify By Gxg : 性能差是因为模式匹配太大太复杂了,会有很多层的嵌套
  87. * ###################
  88. *
  89. * @author ApolloPY <ApolloPY@Gmail.com>
  90. * @param String $strs
  91. * @return String
  92. */
  93. public static function stripBbcodeTags($strs) {
  94. if (empty($strs)) {
  95. return $strs;
  96. }
  97. # 多余的文本分隔符
  98. $strs = preg_replace('#-{3,}|={3,}#', chr(32), $strs);
  99. $strs = preg_replace('#\.{7,}#', '......', $strs);
  100. $strs = str_replace(Convert::gb2u8('【'), chr(32), $strs);
  101. $strs = str_replace(Convert::gb2u8('】'), chr(32), $strs);
  102. $strs = str_replace(Convert::gb2u8('◆'), chr(32), $strs);
  103. $strs = str_replace(Convert::gb2u8('☆'), chr(32), $strs);
  104. $strs = str_replace(Convert::gb2u8('★'), chr(32), $strs);
  105. $strs = str_replace(Convert::gb2u8('�y'), chr(32), $strs);
  106. $strs = str_replace(Convert::gb2u8('�z'), chr(32), $strs);
  107. $strs = str_replace(Convert::gb2u8('�|'), chr(32), $strs);
  108. $strs = str_replace(Convert::gb2u8('�}'), chr(32), $strs);
  109. $strs = str_replace(Convert::gb2u8('��'), chr(32), $strs);
  110. # 多余空格
  111. $strs = str_replace("\xe3\x80\x80", chr(32), $strs); // 过滤一下全角空格
  112. $strs = preg_replace('#\s+#', chr(32), $strs);
  113. # emule
  114. $strs = preg_replace('#\[emule\].*\[/emule\]#Uis', '', $strs);
  115. # img
  116. $strs = preg_replace('#\[img\].*\[/img\]#Uis', '', $strs);
  117. # quote
  118. $strs = self::removeQuote($strs);
  119. // $strs = preg_replace('#\[quote.*\[/quote\]#Uis', '', $strs);
  120. # 遍历 去除 bbcode 标签
  121. $regExp = '#(?:(?:\[(\w+)(?:=.+)?\])|(\[/(\w+)\]))#iUs';
  122. $startMart = array(); // 存放开始标签集合
  123. $endMart = array(); // 存放结束标签集合
  124. preg_match_all($regExp, $strs, $matchs, PREG_SET_ORDER);
  125. foreach ($matchs as $arrMarkInfo) {
  126. if (2 == count($arrMarkInfo)) { // 匹配了 [xxx]
  127. $startMart[] = $arrMarkInfo[1];
  128. } elseif (4 == count($arrMarkInfo)) { // 匹配了 [/xxx]
  129. $endMart[] = $arrMarkInfo[3];
  130. }
  131. }
  132. // 去除重复标签
  133. $startMart = array_unique($startMart);
  134. $endMart = array_unique($endMart);
  135. // 即在开始、又有结束的标签才是需要去除的标签
  136. $arrMarts = array_intersect($startMart, $endMart);
  137. foreach ($arrMarts as $strMark) {
  138. $strs = preg_replace('#\[' . $strMark . '(?:=.+)?\]#Uis', '', $strs);
  139. $strs = str_replace('[/' . $strMark . ']', '', $strs);
  140. }
  141. return trim($strs);
  142. }
  143. /**
  144. * 去除 bbcode 里的 quote 标签以及所有被 quote 标签包含的文本
  145. *
  146. * @param String $str
  147. * @return String
  148. */
  149. public static function removeQuote($str) {
  150. $leftTag = "[quote";
  151. $rightTag = "[/quote]";
  152. $rightTagLen = strlen($rightTag);
  153. $strlen = strlen($str);
  154. // $parsedArr = array();
  155. $remainder = '';
  156. $start = 0;
  157. $l_start = $l_end = $r_start = $r_end = -1;
  158. $level = 0; // stack level
  159. while (true) {
  160. if ($start >= $strlen) {
  161. break;
  162. }
  163. if ($l_start >= $start) {
  164. // no need to search now
  165. } else {
  166. $l_start = strpos($str, $leftTag, $start);
  167. $l_end = strpos($str, "]", $l_start);
  168. }
  169. if ($r_start >= $start) {
  170. // no need to search now
  171. } else {
  172. $r_start = strpos($str, $rightTag, $start);
  173. $r_end = $r_start + $rightTagLen - 1;
  174. }
  175. if ($l_start !== false && $l_end !== false && $r_start !== false && $r_end !== false && $l_end < $r_start) { // tag starter
  176. // before tag starter
  177. if ($level == 0 && $start < $l_start) {
  178. $_str = substr($str, $start, $l_start - $start);
  179. // $parsedArr[] = $_str;
  180. $remainder .= $_str;
  181. }
  182. // $parsedArr[] = substr($str, $l_start, $l_end - $l_start + 1);
  183. $start = $l_end + 1;
  184. $level++;
  185. } else if ($r_start !== false && $r_end != false && $level > 0) { // tag ender
  186. if ($start < $r_start) { // after tag start && before tag end (enclosed in tag)
  187. // $parsedArr[] = substr($str, $start, $r_start - $start);
  188. }
  189. // $parsedArr[] = substr($str, $r_start, $rightTagLen);
  190. $start = $r_end + 1;
  191. $level--;
  192. } else { // no more matches (after matches)
  193. $_str = substr($str, $start);
  194. // $parsedArr[] = $_str;
  195. $remainder .= $_str;
  196. break;
  197. }
  198. }
  199. return $remainder;
  200. }
  201. /**
  202. * 判断字符串中是否含有中文字符
  203. * @author Gxg <gaoxiaogang@gmail.com>
  204. * @param String $str
  205. * @return Boolean
  206. */
  207. static public function isChineseChar($str) {
  208. if (preg_match("/[" . chr(0x80) . "-" . chr(0xff) . "]+/", $str)) {
  209. return true;
  210. } else {
  211. return false;
  212. }
  213. }
  214. /**
  215. * xml转换成数组
  216. * @param string $xml
  217. * @throws Exception 'xml转换成数组失败'
  218. * @author gaoxiaogang@gmail.com
  219. * @return array
  220. */
  221. static public function xmlToArray($xml) {
  222. if (is_string($xml)) {
  223. $tmpResult = simplexml_load_string($xml);
  224. if (!is_object($tmpResult)) {
  225. return array();
  226. }
  227. $tmpArray = (array) $tmpResult;
  228. } elseif (is_object($xml)) {
  229. $tmpArray = (array) $xml;
  230. } else if (is_array($xml)) {
  231. $tmpArray = $xml;
  232. }
  233. foreach ($tmpArray as $tmpK => $tmpV) {
  234. if (count($tmpV) == 0) {
  235. $tmpArray[$tmpK] = '';
  236. } else if (count($tmpV) == 1) {
  237. if (is_object($tmpV)) {
  238. $tmpArray[$tmpK] = self::xmlToArray($tmpV);
  239. } else {
  240. $tmpArray[$tmpK] = (string) $tmpV;
  241. }
  242. } else {
  243. $tmpArray[$tmpK] = self::xmlToArray($tmpV);
  244. }
  245. }
  246. return $tmpArray;
  247. }
  248. }