<?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;
	}
	
	/**
	 * 拼接url和参数,构造新的url
	 * @param string $url 旧的url
	 * @param array $args 需附加到url上的请求参数
	 * @return string
	 */
	static public function jointUrl($url, array $args) {
		$new_url = $url . ((strpos($url, '?') === false) ? '?' : '&') . http_build_query($args);
    	return $new_url;
	}
	
	/**
	 * 以给定显示长度截取字符串,如果执行了截取操作,那么在最后加上$dot
	 * utf-8 下的字宽 一般来说 一个中文是2 一个英文是 1
	 * @author Gxg <gaoxiaogang@Gmail.com>
	 * @param String $text
	 * @param Int $length
	 * @param string $dot
	 * @return String
	 */
	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);
	    }
	}
	
	/**
	 * 增强型mb_strimwidth()函数,如果 $width 为负,支持向前截取字符
	 *
	 * @param String $str
	 * @param Int $start
	 * @param Int $width
	 * @param String $trimmarker
	 * @param String $encoding
	 * @return String
	 */
	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);
	    }
	}
	
	
	/**
     * 去除 bbcode 标签 转换为纯文本
     *
     * ###################
     * ## Modify By Gxg : 性能差是因为模式匹配太大太复杂了,会有很多层的嵌套
     * ###################
     *
     * @author ApolloPY <ApolloPY@Gmail.com>
     * @param String $strs
     * @return String
     */
    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);

        # emule
        $strs = preg_replace('#\[emule\].*\[/emule\]#Uis', '', $strs);

        # img
        $strs = preg_replace('#\[img\].*\[/img\]#Uis', '', $strs);

        # quote
        $strs = self::removeQuote($strs);
//        $strs = preg_replace('#\[quote.*\[/quote\]#Uis', '', $strs);

        # 遍历 去除 bbcode 标签
        $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)) { // 匹配了 [xxx]
                $startMart[] = $arrMarkInfo[1];
            } elseif (4 == count($arrMarkInfo)) { // 匹配了 [/xxx]
                $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);
    }

    /**
     * 去除 bbcode 里的 quote 标签以及所有被 quote 标签包含的文本
     *
     * @param String $str
     * @return String
     */
    public static function removeQuote($str) {
        $leftTag = "[quote";
        $rightTag = "[/quote]";
        $rightTagLen = strlen($rightTag);
        $strlen = strlen($str);
    //    $parsedArr = array();
        $remainder = '';
        $start = 0;
        $l_start = $l_end = $r_start = $r_end = -1;
        $level = 0; // stack level
        while (true) {
            if ($start >= $strlen) {
                break;
            }
            if ($l_start >= $start) {
                // no need to search now
            } else {
                $l_start = strpos($str, $leftTag, $start);
                $l_end = strpos($str, "]", $l_start);
            }
            if ($r_start >= $start) {
                // no need to search now
            } 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) { // tag starter
                // before tag starter
                if ($level == 0 && $start < $l_start) {
                    $_str = substr($str, $start, $l_start - $start);
    //                $parsedArr[] = $_str;
                    $remainder .= $_str;
                }
    //            $parsedArr[] = substr($str, $l_start, $l_end - $l_start + 1);
                $start = $l_end + 1;

                $level++;
            } else if ($r_start !== false && $r_end != false && $level > 0) { // tag ender
                if ($start < $r_start) { // after tag start && before tag end (enclosed in tag)
    //                $parsedArr[] = substr($str, $start, $r_start - $start);
                }
    //            $parsedArr[] = substr($str, $r_start, $rightTagLen);
                $start = $r_end + 1;

                $level--;
            } else { // no more matches (after matches)
                $_str = substr($str, $start);
    //            $parsedArr[] = $_str;
                $remainder .= $_str;
                break;
            }
        }
        return $remainder;
    }

    /**
     * 判断字符串中是否含有中文字符
     * @author Gxg <gaoxiaogang@gmail.com>
     * @param String $str
     * @return Boolean
     */
    static public function isChineseChar($str) {
        if (preg_match("/[" . chr(0x80) . "-" . chr(0xff) . "]+/", $str)) {
            return true;
        } else {
            return false;
        }
    }
    
	/**
	 * xml转换成数组
	 * @param string $xml
	 * @throws Exception 'xml转换成数组失败'
	 * @author gaoxiaogang@gmail.com
	 * @return array
	 */
	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;
	}
}