2011年8月12日 星期五

php 單例設計模式

單例設計模式 今天在網路上看到的一個名詞 說穿了它就是例用static這個關鍵字 其實static早就php4就已經存在了 但很少看到討論它的功能 再順便討論一下吧 ```php function test() { static $a; if ($a == FALSE) { $a = 1; } else { $a++; } echo $a; } test(); //輸出 1 test(); //輸出 2 test(); //輸出 3 test(); //輸出 4 /* 所以當你指定了變數為static時 它就不會因為function執行結束後而將變數回收 所以我們這個例用這個方法來實現單例設計模式 */ class Test { function instance() { static $instance; if ($instance == FALSE) { $instance = new Test(); } return $instance; } } $test =& Test::instance(); $test2 =& Test::instance(); $test3 =& Test::instance(); /* 這樣$test,$test2,$test3都會指向同一個物件 以上為PHP4的寫法 PHP4必須帶參考才能將物件指到同一下 那php5怎麼寫呢? */ class Test { static private $instance; static public function instance() { if (self::$instance == FALSE) { self::$instance = new self; } return self::$instance; } } $test = Test::instance(); /* PHP5的CLASS支援這種寫法當然要用這種寫法了啊! 雖然採用PHP4的寫法也行.... 但新的東西總是要用一下的是不是 */ ```

2011年8月10日 星期三

FB.api究竟要如何使用?

在使用FB的javascript寫應用程式不外乎最常用到的就是FB.api 但查到的文件不外乎都只是用來做資訊查詢... ex. ```javascript FB.api({ method:'fql.query', query:'SELECT uid,first_name,last_name FROM user WHERE is_app_user = 1 AND (uid IN (SELECT uid2 FROM friend WHERE uid1 = me())) ' },function(){ console.info(arguments); }); // 或者是用Graph API來做資訊查詢 ex. FB.api(object_id+"/likes",function(){ console.info(arguments); }); // 但事實上FB.api真正的功能並不只如此 ex. //讚 FB.api(object_id+"/likes","post",function(){ console.info(arguments); }); //取消讚 FB.api(object_id+"/likes","delete"); //publish feed 至 https://graph.facebook.com/arjun/feed FB.api("arjun/feed",{message:'hello'},"post",function(){ console.info(arguments); }); ``` 帶入參數的個數和型態FB.api會自動去判断 善用這些東西很多事情就不用都透過php sdk來執行可以大量減少Server的負擔喔

2011年1月14日 星期五

php date日期加減

寫了那麼久的php我到今天才發現原來日期的增減這麼簡單! 看到之前自己寫的函式才發現自己真的很蠢!哈 ```php $currentDate = date("Y-m-d");// current date echo "Current Date: ".$currentDate; $date30DaysAdded = date('Y-m-d', strtotime("+30 days")); echo "30 Days added: ".$date30DaysAdded; $date10YearsBack = date('Y-m-d', strtotime("-10 years")); echo "10 Years back: ".$date10YearsBack; $date2DaysAdded = date('Y-m-d', strtotime("+2 days")); echo "2 Days Added: ".$date2DaysAdded; ```

2010年12月12日 星期日

php5 json_decode回傳array

在php5內的json_decode的第二個參數決定回傳值為std class或array ```php $json = json_encode(['a' => 'b']); // 回傳 std class var_dump(json_decode ($json)); // 回傳 array var_dump(json_decode($json, true)); ```

2010年11月20日 星期六

PHP 的 XOR

唉!真不知道該說PHP不用指定變數型態這件事情到底好還是不好! 這兩天在寫一段加密的程式,有用到XOR 結果JAVA和PHP的流程部份一模一樣 結果加密後的結果確有些不相同 更扯的是XOR沒辦法自己解密成功 = = 到最後才發現XOR的字元必須用ord做轉換再做XOR 還好在網上有找到別人寫好的程式碼 ```php /** * XOR encrypts a given string with a given key phrase. * * @param string $InputString Input string * @param string $KeyPhrase Key phrase * @return string Encrypted string */ function XOREncryption($InputString, $KeyPhrase){ $KeyPhraseLength = strlen($KeyPhrase); // Loop trough input string for ($i = 0; $i < strlen($InputString); $i++){ // Get key phrase character position $rPos = $i % $KeyPhraseLength; // Magic happens here: $r = ord($InputString[$i]) ^ ord($KeyPhrase[$rPos]); // Replace characters $InputString[$i] = chr($r); } return $InputString; } // Helper functions, using base64 to // create readable encrypted texts: function XOREncrypt($InputString, $KeyPhrase){ $InputString = XOREncryption($InputString, $KeyPhrase); $InputString = base64_encode($InputString); return $InputString; } function XORDecrypt($InputString, $KeyPhrase){ $InputString = base64_decode($InputString); $InputString = XOREncryption($InputString, $KeyPhrase); return $InputString; } ```

2010年10月11日 星期一

chrome font size無法設定小於12px???

今天遇到了一個問題 我將某一區塊的font-size設為10px IE、Firefox都起作用了,但偏偏Chrome沒效果 我就想是不是我打錯字了! 結果最後上google查 Chrome要將字體大小設於小於12px 必須再加上一個webkit獨有的屬性 -webkit-text-size-adjust:none;

2010年8月19日 星期四

如何取得youtube縮圖及id

本來想要每個禮拜都來寫個文章的!結果又偷懶! 哈!又累積好多東西沒寫了,算了,不重要! 今天為了寫出這個網址上的效果http://www.artofchineseliving.com/video 想要利用fancybox顯示youtube影片, 千找萬找!終於讓我找到直接embed的方法(非常的簡單!) 網址只要輸入 [http://www.youtube.com/v/影片id] 即可 可是事實上一般使用者所看到的網址都是 http://www.youtube.com/watch?v=影片id 為了讓使用者在後端方便輸入,並減少資料庫的空間,就寫了一支php程式來取得影片id ```php function video_id($url) { $parse_url = parse_url($url); $query = []; parse_str($parse_url['query'], $query); if (! empty($query['v'])) { return $query['v']; } $t = explode('/', trim($parse_url['path'], '/')); foreach ($t as $k => $v) { if ($v == 'v') { if (! empty($t[$k + 1])) { return $t[$k + 1]; } } } return $url; } echo $video_id = video_id("http://www.youtube.com/watch?v=vXz7O245Row"); echo video_id; //應該會回傳vXz7O245Row,只要將這一串id存入資料庫即可 //在取出的時候就可以做一些應用及變化了 //直接連到youtube echo sprintf('link',$video_id); //利用swfobject embed echo sprintf('new SWFObject("http://www.youtube.com/v/%s", "mymovie", "425", "356", "8", "#ffffff");',$video_id); //取得縮圖 echo sprintf('',$video_id); ```