123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace KIF\String;
- use KIF\Data\Convert;
- class String {
-
- static public function stripslashes($string) {
- if (is_array($string)) {
- foreach ($string as $key => $val) {
- $string[$key] = self::stripslashes($val);
- }
- } else {
- $string = \stripslashes($string);
- }
- return $string;
- }
-
-
- static public function jointUrl($url, array $args) {
- $new_url = $url . ((strpos($url, '?') === false) ? '?' : '&') . http_build_query($args);
- return $new_url;
- }
-
-
- static public function suitLength($text, $length, $middle = false, $dot = '...', $encoding = 'UTF-8') {
- $iLength = mb_strwidth($text, $encoding);
- if ($iLength < $length) {
- return $text;
- }
- if ($middle) {
- if (floor($length / 2) < $length / 2) {
- $iLeftLength = floor($length / 2);
- $iRightLength = $iLeftLength + 1;
- } else {
- $iLeftLength = floor($length / 2);
- $iRightLength = $iLeftLength;
- }
- return mb_strimwidth($text, 0, $iLeftLength, '', $encoding) . $dot . self::yoka_mb_strimwidth($text, mb_strlen($text, $encoding), -$iRightLength, '', $encoding);
- } else {
- return mb_strimwidth($text, 0, $length, $dot, $encoding);
- }
- }
-
-
- static public function yoka_mb_strimwidth($str, $start, $width,$trimmarker='',$encoding=null) {
- if(!isset($encoding)) {
- $encoding = mb_internal_encoding();
- }
- if(0 > $width) {
- $strBack = mb_substr($str,$start,mb_strlen($str,$encoding),$encoding);
- $iBackWidth = mb_strwidth($strBack,$encoding);
- $strFore = mb_substr($str,0,mb_strlen($str,$encoding)-mb_strlen($strBack,$encoding),$encoding);
- $iForeWidth = mb_strwidth($strFore,$encoding);
- if($iForeWidth <= -$width) {
- return $strFore;
- }
- $iForeWidth_1 = $iForeWidth + $width;
- $strFore_1 = mb_strimwidth($strFore,0,$iForeWidth_1,'',$encoding);
- $strFore_2 = mb_substr($strFore, mb_strlen($strFore_1,$encoding), $iForeWidth, $encoding);
- return $strFore_2;
- } else {
- return mb_strimwidth($str, $start, $width, $trimmarker, $encoding);
- }
- }
-
-
-
- public static function stripBbcodeTags($strs) {
- if (empty($strs)) {
- return $strs;
- }
-
- $strs = preg_replace('#-{3,}|={3,}#', chr(32), $strs);
- $strs = preg_replace('#\.{7,}#', '......', $strs);
- $strs = str_replace(Convert::gb2u8('【'), chr(32), $strs);
- $strs = str_replace(Convert::gb2u8('】'), chr(32), $strs);
- $strs = str_replace(Convert::gb2u8('◆'), chr(32), $strs);
- $strs = str_replace(Convert::gb2u8('☆'), chr(32), $strs);
- $strs = str_replace(Convert::gb2u8('★'), chr(32), $strs);
- $strs = str_replace(Convert::gb2u8('�y'), chr(32), $strs);
- $strs = str_replace(Convert::gb2u8('�z'), chr(32), $strs);
- $strs = str_replace(Convert::gb2u8('�|'), chr(32), $strs);
- $strs = str_replace(Convert::gb2u8('�}'), chr(32), $strs);
- $strs = str_replace(Convert::gb2u8('��'), chr(32), $strs);
-
- $strs = str_replace("\xe3\x80\x80", chr(32), $strs);
- $strs = preg_replace('#\s+#', chr(32), $strs);
-
- $strs = preg_replace('#\[emule\].*\[/emule\]#Uis', '', $strs);
-
- $strs = preg_replace('#\[img\].*\[/img\]#Uis', '', $strs);
-
- $strs = self::removeQuote($strs);
-
- $regExp = '#(?:(?:\[(\w+)(?:=.+)?\])|(\[/(\w+)\]))#iUs';
- $startMart = array();
- $endMart = array();
- preg_match_all($regExp, $strs, $matchs, PREG_SET_ORDER);
- foreach ($matchs as $arrMarkInfo) {
- if (2 == count($arrMarkInfo)) {
- $startMart[] = $arrMarkInfo[1];
- } elseif (4 == count($arrMarkInfo)) {
- $endMart[] = $arrMarkInfo[3];
- }
- }
-
- $startMart = array_unique($startMart);
- $endMart = array_unique($endMart);
-
- $arrMarts = array_intersect($startMart, $endMart);
- foreach ($arrMarts as $strMark) {
- $strs = preg_replace('#\[' . $strMark . '(?:=.+)?\]#Uis', '', $strs);
- $strs = str_replace('[/' . $strMark . ']', '', $strs);
- }
- return trim($strs);
- }
-
- public static function removeQuote($str) {
- $leftTag = "[quote";
- $rightTag = "[/quote]";
- $rightTagLen = strlen($rightTag);
- $strlen = strlen($str);
-
- $remainder = '';
- $start = 0;
- $l_start = $l_end = $r_start = $r_end = -1;
- $level = 0;
- while (true) {
- if ($start >= $strlen) {
- break;
- }
- if ($l_start >= $start) {
-
- } else {
- $l_start = strpos($str, $leftTag, $start);
- $l_end = strpos($str, "]", $l_start);
- }
- if ($r_start >= $start) {
-
- } else {
- $r_start = strpos($str, $rightTag, $start);
- $r_end = $r_start + $rightTagLen - 1;
- }
- if ($l_start !== false && $l_end !== false && $r_start !== false && $r_end !== false && $l_end < $r_start) {
-
- if ($level == 0 && $start < $l_start) {
- $_str = substr($str, $start, $l_start - $start);
-
- $remainder .= $_str;
- }
-
- $start = $l_end + 1;
- $level++;
- } else if ($r_start !== false && $r_end != false && $level > 0) {
- if ($start < $r_start) {
-
- }
-
- $start = $r_end + 1;
- $level--;
- } else {
- $_str = substr($str, $start);
-
- $remainder .= $_str;
- break;
- }
- }
- return $remainder;
- }
-
- static public function isChineseChar($str) {
- if (preg_match("/[" . chr(0x80) . "-" . chr(0xff) . "]+/", $str)) {
- return true;
- } else {
- return false;
- }
- }
-
-
- static public function xmlToArray($xml) {
- if (is_string($xml)) {
- $tmpResult = simplexml_load_string($xml);
- if (!is_object($tmpResult)) {
- return array();
- }
- $tmpArray = (array) $tmpResult;
- } elseif (is_object($xml)) {
- $tmpArray = (array) $xml;
- } else if (is_array($xml)) {
- $tmpArray = $xml;
- }
- foreach ($tmpArray as $tmpK => $tmpV) {
- if (count($tmpV) == 0) {
- $tmpArray[$tmpK] = '';
- } else if (count($tmpV) == 1) {
- if (is_object($tmpV)) {
- $tmpArray[$tmpK] = self::xmlToArray($tmpV);
- } else {
- $tmpArray[$tmpK] = (string) $tmpV;
- }
- } else {
- $tmpArray[$tmpK] = self::xmlToArray($tmpV);
- }
- }
- return $tmpArray;
- }
- }
|