2009年3月28日

【PHP】PHP 中的 unescape 函式

   function phpescape($str){

    
$sublen=strlen($str);

    
$reString="";

    
for ($i=0;$i<$sublen;$i++){

        
if(ord($str[$i])>=127){

            
$tmpString=bin2hex(iconv("utf-8","ucs-2",substr($str,$i,2)));    



            
if (!eregi("WIN",PHP_OS)){

                
$tmpString=substr($tmpString,2,2).substr($tmpString,0,2);

            }

            
$reString.="%u".$tmpString;

            
$i++;

        } 
else {

            
$reString.="%".dechex(ord($str[$i]));

        }

    }

    
return $reString;

}





function unescape($str) { 

         
$str = rawurldecode($str); 

         
preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/U",$str,$r); 

         
$ar = $r[0]; 

         
foreach($ar as $k=>$v) { 

                  
if(substr($v,0,2== "%u"

                           
$ar[$k= iconv("UCS-2","utf-8",pack("H4",substr($v,-4))); 

                  
elseif(substr($v,0,3== "&#x"

                           
$ar[$k= iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1))); 

                  
elseif(substr($v,0,2== "&#") { 

                           
$ar[$k= iconv("UCS-2","utf-8",pack("n",substr($v,2,-1))); 

                  } 

         } 

         
return join("",$ar); 









轉貼至:

http://www.phpchina.com/html/91/n-1091.html

2009年2月23日

【ExtJS】Grid 資料可用拖拉方式交換

var ddrow = new Ext.dd.DropTarget(Grid.getView().mainBody, {

            ddGroup : 'ddGroup',

            copy:false,

            notifyDrop : function(ddSource, e, data){

               

                var sm = Grid.getSelectionModel();

                var rows = sm.getSelections();

                var cindex = ddSource.getDragData(e).rowIndex;

                if (!this.copy){

                    for(i = 0; i < rows.length; i++) {

                        ds.remove(ds.getById(rows[i].id));

                    };

                };

                ds.insert(cindex,data.selections);

    

            }

});



grid 別忘了將 enableDragDrop設為true

2009年1月7日

【Javascript】Sleep

function   sleep(n)  {
          var   start=new   Date().getTime();
          while(true)
                 if(new   Date().getTime()-start>n)
                       break;
}





累了就要睡~~@@

2008年12月23日

Setting the HTTP charset parameter

因為太常用到了





所以轉貼至我的網誌





方便日後查詢





以下節錄自:


http://www.w3.org/International/O-HTTP-charset





只紀錄Code部份










<h2>Scripting the header</h2>

The appropriate header can also be set in server side scripting languages. For example:



Perl. Output the correct header before any part of the actual page. After the last header, use a double linebreak, e.g.:


print "Content-Type: text/html; charset=utf-8\n\n";



Python. Use the same solution as for Perl (except that you don't need a semicolon at the end).



PHP. Use the header() function before generating any content, e.g.:


header('Content-type: text/html; charset=utf-8');



Java Servlets. Use the setContentType method on the ServletResponse before obtaining any object (Stream or Writer) used for output, e.g.:


resource.setContentType ("text/html;charset=utf-8");


If you use a Writer, the Servlet automatically takes care of the conversion from Java Strings to the encoding selected.



JSP. Use the page directive e.g.:


<%@ page contentType="text/html; charset=UTF-8" %>


Output from out.println() or the expression elements (<%= object%>) is automatically converted to the encoding selected. Also, the page itself is interpreted as being in this encoding.



ASP and ASP.Net. content type and charset are set independently, and are methods on the response object. To set the charset, use e.g.:


<%Response.charset="utf-8"%>


In ASP.Net, setting Response.ContentEncoding will take care both of the charset parameter in the HTTP Content-Type as well as of the actual encoding of the document sent out (which of course have to be the same). The default can be set in the globalization element in Web.config (or Machine.config, which is originally set to UTF-8).



2008年12月1日

【ExtJS】Ajax Communication failure 解決方法

只需在Ajax failure function 內加入







failure: function(obj1,obj2) {



               if(obj1.statusText == 'communication failure') {



                    var task = new Ext.util.DelayedTask(/*你的ajax code or function*/);



                    task.delay(1);



                }else{



                    alert('請告知管理員伺服器有 : '+obj1.statusText+' 的錯誤');



                }



}



或檢查呼叫的php有無不會顯示訊息的執行錯誤

例如:無限遞迴

2008年9月17日

【PHP】追蹤檔案上傳進度(實作篇)

介紹請看這篇

http://www.wretch.cc/blog/herbjoyce/10148542



仔細閱讀過他們Code之後

發現可以不使用Google API的情況下也可以完成

Google API 的功用主要在於使用Ajax方式Call檔案上傳進度的PHP

所以主要要先安裝APC

如何安裝在介紹頁裡已經講的很詳細

就不在此贅述

安裝完成後

需要一個上傳的介面和一個進度欄(ProgressBar)

ProgressBar我使用的是ExtJS的ProgressBar

var pbar = new Ext.ProgressBar({

        id:'pbar',

        cls:'custom',

        renderTo:'p'

});

需放在網頁最下方(要使網頁load完成後才產生此bar,要不然會出錯)

HTML部分別忘記加上對應的DIV

<div id="p" style="width:300px;"></div>

再來需要一個上傳的form如下:

<form enctype="multipart/form-data" id="upload_form" action="target.php" method="POST">



<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $id?>"/>

<input type="file" id="test_file" name="test_file"/><br/>

<input onclick="startProgress(); return true;" type="submit" value="Upload!"/>



</form>



hidden欄位不可少,那是給上傳檔案一個編號,以便做進度追蹤

$id是使用php function uniqid("") 產生

再來就是使用startProgress()來初始化某些值後開始追蹤上傳進度

function startProgress(){

    setTimeout("getProgress()", 1000);

}

因為我沒有要初值化的東西,所以等待檔案上傳一秒後呼叫getProgress();



var server_url = "getprogress.php?progress_key=<?php echo($id)?>";

function getProgress(){

   

    http_request = null;

    url = server_url;

   

    //嘗試使用各種方式開啟不同瀏覽器的http_request

    if (window.XMLHttpRequest) { // Mozilla, IE7, Safari,...

        http_request = new XMLHttpRequest();

        if (http_request.overrideMimeType) {

            http_request.overrideMimeType('text/xml');

        }

           http_request.open('GET', url, 'false');     

    } else if (window.ActiveXObject) { // IE6

        try {

            http_request = new ActiveXObject("Msxml2.XMLHTTP");

            http_request.open('GET', url, 'false');     

        } catch (e) {

            try {

                http_request = new ActiveXObject("Microsoft.XMLHTTP");

                http_request.open('GET', url, 'false');     

            } catch (e) {}

        }

    }



    if (!http_request) {

        alert('Giving up :( Cannot create an XMLHTTP instance');

        return false;

    }

    //開啟後便呼叫doCallback取得response

    http_request.onreadystatechange = doCallback;       

    http_request.send(null);

   

    function doCallback() {



        if (http_request.readyState == 4) {

            if (http_request.status == 200) {

               

                if(http_request.responseText != "done"){

                    //在此取得並設定%數

                    percent = http_request.responseText;

                    pbar.updateProgress((percent/100), Math.round(percent)+'% completed...');

                    if (percent < 100){

                        setTimeout("getProgress(server_url)", 100);

                    }

                }

               

                //if(msg!=null)alert(msg);                      

            } else {

                //document.getElementById(target).innerHTML = http_request.responseText;

                alert('There was a problem with the request.');

            }

        }

       

    }

}



到此大致上就完成了





==============================================================



使用ExtJS framework 完成 getProgress()



var server_url = "getprogress.php?progress_key=<?php echo($id)?>";

function getProgress(){

   

    Ext.Ajax.request({

            url: server_url,

            method: 'POST',

            success: function(result) {

                percent = result.responseText;

                pbar.updateProgress((percent/100), Math.round(percent)+'% completed...');

                if (percent < 100){

                        setTimeout("getProgress()", 100);

                }

            },

            failure: function() {

               alert('伺服器錯誤,請聯絡管理員'); 

            }

    });

}

2008年8月15日

【PHP】Cookies失效

說來真是可笑







php寫這麼久







第一次遇上cookie無效的囧境







原來在下層目錄的php使用setcookie所建立的cookie







無法承襲至上層目錄內的php使用 ....







好險沒花我太多時間去處理這個







要不然可能又是一段艱辛的Debug過程吧







=============================================



10/12



int setcookie (string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])



setcookie可設定有效路徑......