Xunsearch PHP-SDK v1.3.2 API 参考文档

XSUtil

XS.util
继承关系 class XSUtil
版本 1.0.0
源代码 sdk/php/util/XSUtil.class.php
XSUtil 工具程序通用代码

Public 方法

隐去继承来的方法

名称描述定义于
convertIn() 把用户输入的字符串转换为 UTF-8 编码 XSUtil
convertOut() 把 UTF-8 字符串转换为用户编码 XSUtil
fixWidth() 修正字符串至固定宽度 XSUtil
flush() 刷新标准输出缓冲区 XSUtil
getOpt() 取得命令行参数 XSUtil
parseOpt() 解析命令行参数 XSUtil
setCharset() 设置输出、输入编码 XSUtil

方法明细

convertIn() 方法
public static string convertIn(string $buf)
$buf string 要转换字符串
{return} string 转换后的字符串
源码: sdk/php/util/XSUtil.class.php#L78 (显示)
public static function convertIn($buf)
{
    if (
self::$charset !== null)
        return 
XS::convert($buf'UTF-8'self::$charset);
    return 
$buf;
}

把用户输入的字符串转换为 UTF-8 编码

convertOut() 方法
public static string convertOut(string $buf)
$buf string 要转换字符串
{return} string 转换后的字符串
源码: sdk/php/util/XSUtil.class.php#L66 (显示)
public static function convertOut($buf)
{
    if (
self::$charset !== null)
        return 
XS::convert($bufself::$charset'UTF-8');
    return 
$buf;
}

把 UTF-8 字符串转换为用户编码

fixWidth() 方法
public static type fixWidth(string $text, int $size, string $pad=' ')
$text string 要修正的字符串
$size int 修正的目标宽度
$pad string 用于填充补足的字符
{return} type
源码: sdk/php/util/XSUtil.class.php#L32 (显示)
public static function fixWidth($text$size$pad ' ')
{
    for (
$i $j 0$i strlen($text) && $j $size$i++, $j++)
    {
        if ((
ord($text[$i]) & 0xe0) == 0xe0)
        {
            if ((
$size $j) == 1)
                break;
            
$j++;
            
$i += 2;
        }
    }
    return 
substr($text0$i) . str_repeat($pad$size $j);
}

修正字符串至固定宽度 其中一个全角符号、汉字的宽度为半角字符的 2 倍。

flush() 方法
public static void flush()
源码: sdk/php/util/XSUtil.class.php#L179 (显示)
public static function flush()
{
    
flush();
    if (
ob_get_level() > 0)
        
ob_flush();
}

刷新标准输出缓冲区

getOpt() 方法
public static string getOpt(string $short, string $long=NULL, bool $extra=false)
$short string 短参数名
$long string 长参数名
$extra bool 是否补用默认顺序的参数
{return} string 返回可用的参数值,若不存在则返回 null
源码: sdk/php/util/XSUtil.class.php#L157 (显示)
public static function getOpt($short$long null$extra false)
{
    if (
self::$options === null)
        
self::parseOpt();

    
$value null;
    
$options self::$options;
    if (
$long !== null && isset($options[$long]))
        
$value $options[$long];
    else if (
$short !== null && isset($options[$short]))
        
$value $options[$short];
    else if (
$extra === true && isset($options['-'][self::$optind]))
    {
        
$value $options['-'][self::$optind];
        
self::$optind++;
    }
    return 
$value;
}

取得命令行参数 要求事先调用 parseOpt, 否则会自动以默认参数调用它。

参见

parseOpt() 方法
public static array parseOpt(array $valued=array ( ))
$valued array 需要附加值的参数列表
{return} array 解析完的参数数组,未指定 - 开头的选项统一放入 '-' 的子数组
源码: sdk/php/util/XSUtil.class.php#L90 (显示)
public static function parseOpt($valued = array())
{
    
$result = array('-' => array());
    
$params = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
    for (
$i 0$i count($params); $i++)
    {
        if (
$params[$i] === '--')
        {
            for (
$i $i 1$i count($params); $i++)
                
$result['-'][] = $params[$i];
            break;
        }
        else if (
$params[$i][0] === '-')
        {
            
$value true;
            
$pname substr($params[$i], 1);
            if (
$pname[0] === '-')
            {
                
$pname substr($pname1);
                if ((
$pos strpos($pname'=')) !== false)
                {
                    
$value substr($pname$pos 1);
                    
$pname substr($pname0$pos);
                }
            }
            else if (
strlen($pname) > 1)
            {
                for (
$j 1$j strlen($params[$i]); $j++)
                {
                    
$pname substr($params[$i], $j1);
                    if (
in_array($pname$valued))
                    {
                        
$value substr($params[$i], $j 1);
                        break;
                    }
                    else if ((
$j 1) != strlen($params[$i]))
                    {
                        
$result[$pname] = true;
                    }
                }
            }
            if (
$value === true && in_array($pname$valued) && isset($params[$i 1]))
            {
                
$value $params[$i 1];
                
$i++;
            }
            
$result[$pname] = $value;
        }
        else
        {
            
$result['-'][] = $params[$i];
        }
    }
    
self::$options $result;
    
self::$optind 1;
    return 
$result;
}

解析命令行参数

setCharset() 方法
public static void setCharset(string $charset)
$charset string 期望得到的字符集
源码: sdk/php/util/XSUtil.class.php#L52 (显示)
public static function setCharset($charset)
{
    if (
$charset !== null && strcasecmp($charset'utf8') && strcasecmp($charset'utf-8'))
    {
        
self::$charset $charset;
        
ob_start(array(__CLASS__'convertOut'));
    }
}

设置输出、输入编码 默认输出的中文编码均为 UTF-8

Copyright © 2008-2011 by 杭州云圣网络科技有限公司
All Rights Reserved.