2009/06/08

php探测编码

<?php

//探测编码。注意:由于gb2312,gbk,big5范围有重合,按照最小范围判断
function get_encoding($string)
{

    $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re
['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re
['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re
['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
   
   
foreach( $re as $encoding=>$pattern )
    {

        $rstring = preg_replace($pattern, "", $string);
       
if( $rstring == "")
        {

            return $encoding;
        }

    }
    return null;
}


?>

注意:遇到内有乱码或者错误字符的情况未作测试,例如半个汉字的情形。

 

php探测字符编码

<DIV style="BACKGROUND: #fdfdfd; COLOR: black"><U>PHP语言</U>: <A
href="http://fayaa.com/code/view/1934/">Codee#1934</A></DIV>
<DIV class=source style="COLOR: #000000; FONT-FAMILY:
'[object]','Consolas','Lucida Console','Courier New';
BACKGROUND-COLOR: #f9f7ed" jQuery1244450739390="2"><SPAN style="COLOR:
#008080">&lt;?php</SPAN><BR><BR><SPAN style="COLOR: #008800;
FONT-STYLE: italic">//探测编码。注意:由于gb2312,gbk,big5范围有重合,按照最小范围判断</SPAN><BR><SPAN
style="FONT-WEIGHT: bold; COLOR: #000080">function</SPAN> <SPAN
style="COLOR: #000000">get_encoding</SPAN>(<SPAN style="COLOR:
#000000">$string</SPAN>)<BR><SPAN style="COLOR:
#000000">{</SPAN><BR>&nbsp;&nbsp;&nbsp; <SPAN style="COLOR:
#000000">$re</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN
style="COLOR: #0000ff">&#39;utf-8&#39;</SPAN><SPAN style="COLOR:
#000000">]</SPAN> <SPAN style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #0000ff">&quot;/[</SPAN><SPAN style="COLOR:
#0000ff">\x01</SPAN><SPAN style="COLOR: #0000ff">-</SPAN><SPAN
style="COLOR: #0000ff">\x7f</SPAN><SPAN style="COLOR:
#0000ff">]|[</SPAN><SPAN style="COLOR: #0000ff">\xc2</SPAN><SPAN
style="COLOR: #0000ff">-</SPAN><SPAN style="COLOR:
#0000ff">\xdf</SPAN><SPAN style="COLOR: #0000ff">][</SPAN><SPAN
style="COLOR: #0000ff">\x80</SPAN><SPAN style="COLOR:
#0000ff">-</SPAN><SPAN style="COLOR: #0000ff">\xbf</SPAN><SPAN
style="COLOR: #0000ff">]|[</SPAN><SPAN style="COLOR:
#0000ff">\xe0</SPAN><SPAN style="COLOR: #0000ff">-</SPAN><SPAN
style="COLOR: #0000ff">\xef</SPAN><SPAN style="COLOR:
#0000ff">][</SPAN><SPAN style="COLOR: #0000ff">\x80</SPAN><SPAN
style="COLOR: #0000ff">-</SPAN><SPAN style="COLOR:
#0000ff">\xbf</SPAN><SPAN style="COLOR: #0000ff">]{2}|[</SPAN><SPAN
style="COLOR: #0000ff">\xf0</SPAN><SPAN style="COLOR:
#0000ff">-</SPAN><SPAN style="COLOR: #0000ff">\xff</SPAN><SPAN
style="COLOR: #0000ff">][</SPAN><SPAN style="COLOR:
#0000ff">\x80</SPAN><SPAN style="COLOR: #0000ff">-</SPAN><SPAN
style="COLOR: #0000ff">\xbf</SPAN><SPAN style="COLOR:
#0000ff">]{3}/&quot;</SPAN>;<BR>&nbsp;&nbsp;&nbsp; <SPAN style="COLOR:
#000000">$re</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN
style="COLOR: #0000ff">&#39;gb2312&#39;</SPAN><SPAN style="COLOR:
#000000">]</SPAN> <SPAN style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #0000ff">&quot;/[</SPAN><SPAN style="COLOR:
#0000ff">\x01</SPAN><SPAN style="COLOR: #0000ff">-</SPAN><SPAN
style="COLOR: #0000ff">\x7f</SPAN><SPAN style="COLOR:
#0000ff">]|[</SPAN><SPAN style="COLOR: #0000ff">\xb0</SPAN><SPAN
style="COLOR: #0000ff">-</SPAN><SPAN style="COLOR:
#0000ff">\xf7</SPAN><SPAN style="COLOR: #0000ff">][</SPAN><SPAN
style="COLOR: #0000ff">\xa0</SPAN><SPAN style="COLOR:
#0000ff">-</SPAN><SPAN style="COLOR: #0000ff">\xfe</SPAN><SPAN
style="COLOR: #0000ff">]/&quot;</SPAN>;<BR>&nbsp;&nbsp;&nbsp; <SPAN
style="COLOR: #000000">$re</SPAN><SPAN style="COLOR:
#000000">[</SPAN><SPAN style="COLOR:
#0000ff">&#39;gbk&#39;</SPAN><SPAN style="COLOR: #000000">]</SPAN>
<SPAN style="COLOR: #000000">=</SPAN> <SPAN style="COLOR:
#0000ff">&quot;/[</SPAN><SPAN style="COLOR: #0000ff">\x01</SPAN><SPAN
style="COLOR: #0000ff">-</SPAN><SPAN style="COLOR:
#0000ff">\x7f</SPAN><SPAN style="COLOR: #0000ff">]|[</SPAN><SPAN
style="COLOR: #0000ff">\x81</SPAN><SPAN style="COLOR:
#0000ff">-</SPAN><SPAN style="COLOR: #0000ff">\xfe</SPAN><SPAN
style="COLOR: #0000ff">][</SPAN><SPAN style="COLOR:
#0000ff">\x40</SPAN><SPAN style="COLOR: #0000ff">-</SPAN><SPAN
style="COLOR: #0000ff">\xfe</SPAN><SPAN style="COLOR:
#0000ff">]/&quot;</SPAN>;<BR>&nbsp;&nbsp;&nbsp; <SPAN style="COLOR:
#000000">$re</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN
style="COLOR: #0000ff">&#39;big5&#39;</SPAN><SPAN style="COLOR:
#000000">]</SPAN> <SPAN style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #0000ff">&quot;/[</SPAN><SPAN style="COLOR:
#0000ff">\x01</SPAN><SPAN style="COLOR: #0000ff">-</SPAN><SPAN
style="COLOR: #0000ff">\x7f</SPAN><SPAN style="COLOR:
#0000ff">]|[</SPAN><SPAN style="COLOR: #0000ff">\x81</SPAN><SPAN
style="COLOR: #0000ff">-</SPAN><SPAN style="COLOR:
#0000ff">\xfe</SPAN><SPAN style="COLOR: #0000ff">]([</SPAN><SPAN
style="COLOR: #0000ff">\x40</SPAN><SPAN style="COLOR:
#0000ff">-</SPAN><SPAN style="COLOR: #0000ff">\x7e</SPAN><SPAN
style="COLOR: #0000ff">]|</SPAN><SPAN style="COLOR:
#0000ff">\xa1</SPAN><SPAN style="COLOR: #0000ff">-</SPAN><SPAN
style="COLOR: #0000ff">\xfe</SPAN><SPAN style="COLOR:
#0000ff">])/&quot;</SPAN>;<BR>&nbsp;&nbsp;&nbsp;
<BR>&nbsp;&nbsp;&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR:
#000080">foreach</SPAN>( <SPAN style="COLOR: #000000">$re</SPAN> <SPAN
style="FONT-WEIGHT: bold; COLOR: #000080">as</SPAN> <SPAN
style="COLOR: #000000">$encoding</SPAN><SPAN style="COLOR:
#000000">=&gt;</SPAN><SPAN style="COLOR: #000000">$pattern</SPAN>
)<BR>&nbsp;&nbsp;&nbsp; <SPAN style="COLOR:
#000000">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN
style="COLOR: #000000">$rstring</SPAN> <SPAN style="COLOR:
#000000">=</SPAN> <SPAN style="COLOR:
#000000">preg_replace</SPAN>(<SPAN style="COLOR:
#000000">$pattern</SPAN><SPAN style="COLOR: #000000">,</SPAN> <SPAN
style="COLOR: #0000ff">&quot;&quot;</SPAN><SPAN style="COLOR:
#000000">,</SPAN> <SPAN style="COLOR:
#000000">$string</SPAN>);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<SPAN style="FONT-WEIGHT: bold; COLOR: #000080">if</SPAN>( <SPAN
style="COLOR: #000000">$rstring</SPAN> <SPAN style="COLOR:
#000000">==</SPAN> <SPAN style="COLOR:
#0000ff">&quot;&quot;</SPAN>)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<SPAN style="COLOR:
#000000">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<SPAN style="FONT-WEIGHT: bold; COLOR: #000080">return</SPAN> <SPAN
style="COLOR: #000000">$encoding</SPAN>;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<SPAN style="COLOR: #000000">}</SPAN><BR>&nbsp;&nbsp;&nbsp; <SPAN
style="COLOR: #000000">}</SPAN><BR>&nbsp;&nbsp;&nbsp; <SPAN
style="FONT-WEIGHT: bold; COLOR: #000080">return</SPAN> <SPAN
style="FONT-WEIGHT: bold; COLOR: #000080">null</SPAN>;<BR><SPAN
style="COLOR: #000000">}</SPAN><BR><BR><SPAN style="COLOR:
#008080">?&gt;</SPAN><BR></DIV>

2009/04/23

php simplexml object to array

一般使用simplexml来解析xml文档,比较方便,但有时侯需要把文档转为php的标准数组来操作。

下面写了一个函数用来把simplexml对象转换为数组,支持SimpleXMLElement和SimpleXMLIterator对象。


<?php

//simplexml文档对象转换为标准数组
function simplexml_obj2array($obj)
{   
    if( count($obj) >= 1 )
    {
        $result = $keys = array();
       
        foreach( $obj as $key=>$value)
        {  
            isset($keys[$key]) ? ($keys[$key] += 1) : ($keys[$key] = 1);
           
            if( $keys[$key] == 1 )
            {
                $result[$key] = simplexml_obj2array($value);
            }
            elseif( $keys[$key] == 2 )
            {
                $result[$key] = array($result[$key], simplexml_obj2array($value));
            }
            else if( $keys[$key] > 2 )
            {
                $result[$key][] = simplexml_obj2array($value);
            }
        }
        return $result;
    }
    else if( count($obj) == 0 )
    {
        return (string)$obj;
    }
}
?>


示例:
<?php

$string = '<?xml version="1.0" encoding="UTF-8"?> 
<root>
<name><![CDATA[三叶草]]></name>
<url><![CDATA[http://bloomandkevin.cn]]></url>
<items>
    <product>
        <name><![CDATA[三叶草]]></name>
        <url><![CDATA[http://bloomandkevin.cn]]></url>
    </product>
    <product>
        <name><![CDATA[三叶草]]></name>
        <url><![CDATA[http://bloomandkevin.cn]]></url>
    </product>
</items>
</root>
';

var_dump( simplexml_obj2array(simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA)));

?>


结果:
array(3) {
  ["name"]=>
  string(9) "三叶草"
  ["url"]=>
  string(23) "http://bloomandkevin.cn"
  ["items"]=>
  array(1) {
    ["product"]=>
    array(2) {
      [0]=>
      array(2) {
        ["name"]=>
        string(9) "三叶草"
        ["url"]=>
        string(23) "http://bloomandkevin.cn"
      }
      [1]=>
      array(2) {
        ["name"]=>
        string(9) "三叶草"
        ["url"]=>
        string(23) "http://bloomandkevin.cn"
      }
    }
  }
}


缺点:
丢失了XML节点中的属性值。

2009/04/20

Linux下PHP保持进程唯一运行


<?php

//确认进程唯一,防止死掉
function check_proc() {
    if( $pidfile = @file_get_contents("./my.pid") )
    {
        list($pid, $last_time) = explode(",", $pidfile);       
        if( $link = @readlink("/proc/{$pid}/exe") )
        {
            if( time()-$last_time > 60*60 )//1 hour
            {
                //超时KILL掉那个进程
                system("kill -9 {$pid}");
            }else{
                echo "already exist, process exit.\n";
                exit;
            }
        }
    }
    //写入当前PID信息
    file_put_contents("./my.pid", getmypid().','.time() );
}

?>


getmypid()获取进程的ID。

在PHP程序起始处运行此函数,即可确保进程唯一,且可以踢掉超时的进程。

也可以使用shell_exec("ps aux|grep ".$pid)来判断进程是否存在。对Linux越熟悉就越多方法。

2009/03/31

php一行搞定过滤字符


<?php
iconv('GBK', 'GBK//IGNORE', $file);//过滤掉错误字符,防止解析错误
?>


解决问题:如果你的文件中含有错误字符,在执行解析xml等操作时有时会引发解析错误。在解析xml文档时若头部编码为gb2312,尽量替换为gbk。

方法:iconv,将进出编码均设置为同一编码即可,加上'//IGNORE'即可。

优点:省去自己解析字串剔除错误编码,非常方便。

应用场景:

1.使用simplexml_load_file(),simplexml_load_string(),出现错误Premature end of data in tag...。改用file_get_contents(),然后iconv(),再simplexml_load_string()即可。

2.Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]:
input conversion failed due to input error, bytes 0xC2 0x2E 0x20 0x20 in。。。

2009/03/23

黑莓所有按键失效

昨天我的黑莓所有按键全部失效了,原因是因为轨迹球下方的触点移位了,导致触点一直处于按下状态,所以其它按键通通无效了。

送修也是很郁闷,中关村鼎好地下一层,虽然卖家人品还好,免费给维修了。但修理的结果确实郁闷,拧裂了机壳,太暴力了,电池后盖合上还总有缝,键盘有两个键也大不如以前好使了。泪奔。。。我的882啊

以为很大的问题呢,如果知道只是这么一点小问题,我就自己在家修了。

学到一点小技巧,处理手机内屏的灰尘时,用胶带粘去即可,注意胶的粘性不可太强,否则会适得其反会把胶留在内屏里头。以前我拆了手机总是处理不掉里面的灰尘。手机在保质期内还是送修吧,别自己把问题搞大了,还无法保修。

EACCELERATOR导致Apache宕机

最近服务器总出现apache宕机,错误日志总出现类似的记录,EACCELERATOR: PHP crashed on opline...
例:
[2332] EACCELERATOR: PHP crashed on opline 73 of query() at /XXX/app/vendors/classes_common.php:294

解决办法:
1.eaccelerator.filter选项中过滤掉出错的文件。
我尝试了,但开启debug后,在日志里依然可以发现过滤文件的踪影,郁闷。

2.关掉eaccelerator。PHP5.2.6后的版本已经优化的很不错了,加上zend出的加速器,速度还可以。

配置:apache 2.2.4 + php 5.2.5 + windows server 2003



参考
[1]http://bbs.laxjyj.com/read-htm-tid-4678360.html

2009/03/21

动心是否值得?

佛云人有七苦,生、老、病、死、爱别离、怨憎会、求不得。