apache中文url日志分析--php十六进制字符串转换

#! /usr/local/bin/php
<?php
while (($line = fgets(STDIN)) !== false) {
    $str = urldecode(trim($line));
    //$line = '\xba\xbd\xcc\xec\xb3\xc7';
    //$line = '\xb9\xc9\xca\xd0';
    //$line = '%E4%B8%9C%E6%96%B9%E9%98%B3%E5%85%89%E4%B8%8E%E6%AC%A7%E9%A3%8E%E7%BE%8E%E9%9B%A8%EF%BC%81';
    $str = str_replace(array("\t","\n","\r"),'',stripcslashes($str));
    echo mb_convert_encoding($str, 'GB2312', mb_detect_encoding($str , array('UTF-8','GB2312', 'GBK','LATIN1','BIG5')));
}

/**
function is_utf8($word)
{
    if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function is_gb2312($str)
{
        for($i=0; $i<strlen($str); $i++) {
                $v = ord( $str[$i] );
                if( $v > 127) {
                        if( ($v >= 228) && ($v <= 233) )
                        {
                                if( ($i+2) >= (strlen($str) - 1)) return true;  // not enough characters
                                $v1 = ord( $str[$i+1] );
                                $v2 = ord( $str[$i+2] );
                                if( ($v1 >= 128) && ($v1 <=191) && ($v2 >=128) && ($v2 <= 191) ) // utf编码
                                        return false;
                                else
                                        return true;
                        }
                }
        }
        return true;
}


*/
?>

你可能感兴趣的:(PHP,职场,休闲)