2010年4月27日 星期二

常用的PHP function

在php開發中,我們經常會遇到一些函數,下面是整理好的,可以直接使用,方便開發效率,不過建議大家一定要先看懂,然後在這個基礎上增加功能就提高了自身能力了

1.產生隨機字符串函數

function random($length) {

$hash = @#@#;

$chars = @#abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz@#;

$max = strlen($chars) - 1;

mt_srand((double)microtime() * 1000000);

for($i = 0; $i < $length; $i ) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } 2.截取一定長度的字符串 注:該函數對gb2312使用有效 function wordscut($string, $length ,$sss=0) { if(strlen($string) > $length) {

if($sss){

$length=$length - 3;

$addstr=@# ...@#;

}

for($i = 0; $i < $length; $i ) { if(ord($string[$i]) > 127) {

$wordscut .= $string[$i].$string[$i 1];

$i ;

} else {

$wordscut .= $string[$i];

}

}

return $wordscut.$addstr;

}

return $string;

}

3.取得客戶端ip 地址

function getip(){

if (getenv("http_client_ip") && strcasecmp(getenv("http_client_ip"), "unknown"))

$ip = getenv("http_client_ip");

else if (getenv("http_x_forwarded_for") && strcasecmp(getenv("http_x_forwarded_for"), "unknown"))

$ip = getenv("http_x_forwarded_for");

else if (getenv("remote_addr") && strcasecmp(getenv("remote_addr"), "unknown"))

$ip = getenv("remote_addr");

else if (isset($_server[@#remote_addr@#]) && $_server[@#remote_addr@#] && strcasecmp($_server[@#remote_addr@#], "unknown"))
$ip = $_server[@#remote_addr@#];

else

$ip = "unknown";

return($ip);

}
4. 創建相應的文件夾

function createdir($dir=@#@#)

{

if (!is_dir($dir))

{

$temp = explode(@#/@#,$dir);

$cur_dir = @#@#;

for($i=0;$i
if(!empty($url))

$html .="";

$html .="";

$html .="



";

$html .="";

$html .="";

$html .="";

$html .="";

$html .="".$title."";

$html .="";

$html .="
".$message."

";

if (!empty($url))

$html .="系統將在3秒後返回
如果您的瀏覽器不能自動返回,請點擊[這裡]進入";
else

$html .="[返回]";

$html .="";

$html .="";

echo $html;

exit;

}

7. 分頁(兩個函數配合使用)

function getpage($sql,$page_size=20)

{
global $page,$totalpage,$sums; //out param

$page = $_get["page"];

//$eachpage = $page_size;

$pagesql = strstr($sql," from ");

$pagesql = "select count(*) as ids ".$pagesql;

$result = mysql_query($pagesql);

if($rs = mysql_fetch_array($result)) $sums = $rs[0];

$totalpage = ceil($sums/$page_size);

if((!$page)||($page<1)) $page=1; $startpos = ($page-1)*$page_size; $sql .=" limit $startpos,$page_size "; return $sql; } function showbar($string="") { global $page,$totalpage; $out="共& lt;font ".$totalpage."color=@#red@#>".$totalpage."< /font>頁";

$linknum =4;
$start = ($page-round($linknum/2))>0 ? ($page-round($linknum/2)) : "1";

$end = ($page round($linknum/2))<$totalpage ? ($page round($linknum/2)) : $totalpage; $prestart=$start-1; $nextend=$end 1; if($page<>1)

$out .= "第一頁 ";

if($start>1)

$out.="..<< ";

for($t=$start;$t<=$end;$t ) { $out .= ($page==$t) ? "[".$t."] " : "$t ";
}

if($end<$totalpage) $out.=">>..";

if($page<>$totalpage)

$out .= " 最後頁";

return $out;

}

8.獲取新插入數據的 id

2010年4月26日 星期一

帥氣的輸入框樣式

平淡無奇的輸入框,看到膩了嗎?

嘗試一下
http://buildinternet.com/2009/01/changing-form-input-styles-on-focus-with-jquery/

DEMO頁面
http://buildinternet.com/live/jqueryform/jqueryform1.php?status=20

透過Jquery及CSS就可以讓你的輸入框,自動對齊。

如標題!!
想自動對齊,請服用。
http://www.jankoatwarpspeed.com/post/2008/07/09/Justify-elements-using-jQuery-and-CSS.aspx

jquery 自動完成 autoComplete插件

紀錄一下各參數的意思!!
http://ltl3884.javaeye.com/blog/512738

2010年4月25日 星期日

Zend Framework中,View的From組件

如PEAR一樣,ZF的View也有好用的From組件

$this->formText('date' , '',array('size'=>4))




以上就是,建立一個id為Date,

name也是Date


無預設內容,文字長度為4的輸入框。

2010年4月23日 星期五

檢查數值是否存在於陣列的Key或Value

在PHP中常用的in_array是用來檢查

$array = array("a", "b, "c");


in_array("a",$array);

這樣結果為真,那當你想知道是否在鍵中可以用

$array = array("a"=>0, "b"=>1, "c"=>0);

array_key_exists("a", $array)

結果為真

2010年4月20日 星期二

查詢中文編碼,的資料庫

http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=%E6%96%87


Decimal UTF-8 UTF-16 UTF-32

2010年4月13日 星期二

使用PHP來做人臉辨識

透過利用OPENCV來製作人臉辨識的效果



http://ithelp.ithome.com.tw/question/10036727

2010年4月6日 星期二

Notepad++

Notepad++
自動完成



quicktext

結合!!超強

2010年4月5日 星期一

在GAE中寫php

介紹
http://www.youtube.com/watch?v=bXZlF-HPSq8&feature=related

用Google App Engine跑CodeIgniter

http://fillano.blog.ithome.com.tw/post/257/25696

在GAE中下SQL語法jiql

http://www.jiql.org/xwiki/bin/view/Main/