极湖

无不用其“极”

用于分割字符串的两个 PHP 函数

,

新作了两个函数,结果没用上,放在在这儿以备用。
/**
 * 按词分割字符串
 * @param $str 元字符串
 * @param $maxLength 单词最大长度
 * @return array
 */
function splitInWords($str, $maxLength = 16) {
    if(empty($str)) return array();
    if($maxLength <= 0) $maxLength = 1;
    $rawWords = explode(' ', $str);
    $words = array();
    foreach($rawWords as $word) {
        $len = mb_strlen($word);
        if($len > $maxLength) {
            $pos = 0;
            while($len > $maxLength) {
                $words[] = mb_substr($word, $pos, $maxLength);
                $pos += $maxLength;
                $len -= $maxLength;
            }
            $words[] = mb_substr($word, $pos, $maxLength);
        } else {
            $words[] = $word;
        }
    }
    return $words;
}

/**
 * 按行(指定长度)分割字符串
 * @param $str 元字符串
 * @param $length 每行最大字符数
 * @return array
 */
function splitInLines($str, $length = 80) {
    $lines = array();
    foreach(split("\n", $str) as $s) {
        preg_match_all("/./u", $s, $matches);
        $arr = $matches[0];
        for($i = 0; $i < ceil(count($arr)/$length); $i++) {
            $lines[] = join("", array_slice($arr, $i*$length, $length));
        }
    }
    return $lines;
}

给 ExtJS 的输入控件增加提示功能Zend Framework 下实现简单的 proxy 功能

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.

February 2012
S M T W T F S
January 2012March 2012
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29