網頁

顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2013年6月6日

Install PHP libevent

Install libevent core library
apt-get install libevent-dev

you will need to install PEAR via apt-get to get the necessary package and distribution system that both PEAR and PECL use.
apt-get install php-pear
Now you will need to install the php5-dev package to get the necessary PHP5 source files to compile additional modules
pecl install libevent
If you get some warning message, try this command
pecl install channel://pecl.php.net/libevent-0.1.0

After install libevent, remember to modify php.ini, add this line
extension=libevent.so
You can use this command to check if php already enable libevent
php -m | grep libevent

2013年5月25日

Timezone problem

目前遇到 timezone 設定上的問題,大致上是這樣的。

如果有一個全球性的網站,網站上面會顯示時間,時間必須以 local time 表示。那麼,DB、PHP 裡的 timezone 應該怎麼設定呢?一般在台灣安裝的 DB、PHP 都是以 Asia/Taipei 都做 timezone 設定,也就是 GMT+8。

但是轉換到其他的 local time 時,就顯得不夠直覺。所以目前的想法是將全部都設定成 GMT+0,當作一個標準的時間。然後根據使用者的環境,轉換到使用者的 local time 。

Postgres

/etc/postgresql/9.1/main/postgresql.conf
timezone = GMT+0’
/etc/init.d/postgresql restart

PHP

新增一個 ini 檔案,放到 /etc/php5/conf.d/ 路徑下
/etc/php5/conf.d/timezone.ini
date.timezone = "GMT+0"
/etc/init.d/apache2 restart

User Timezone

使用者的 timezone 實作上有多種方法,因為要知道使用者的 timezone 是一件滿困難的事情,另外就是準確度的問題。

1. GIO IP。透過使用者的 ip 做定位,然後轉換成對應的 timezone。這種方法會遇到準確度的問題。

2. 寫AJAX CODE,讀取 Browsr 的 time 傳回給server,然後由server 判斷時間差了多久。間接猜測使用者的 timezone。

3. 在網頁上開一個設定頁面,讓使用者設定 prefer timezone,server 依靠這個值轉換出正確的 local time。

2013年5月19日

Accurate Regex Pattern for Matching URLs

Original: An Improved Liberal, Accurate Regex Pattern for Matching URLs
Test Data: http://daringfireball.net/misc/2010/07/url-matching-regex-test-data.text

這一篇文章的版權屬於原作者,這裡只是做個程式上的學習記錄。

因為最近在開發wall script,可以讓使用者在 textarea 中輸入一段文字,假如文字中遇到 url pattern,後端的 php 可以自動幫 url 加上 anchor tag。之後顯示在覽劉器中,就會自動變成一個超連結了。

問題的描述滿簡單的,但是要怎樣判斷 url ,就變得十分的棘手。遍尋網路上的一些解答,以目前這一篇的解法最有邏輯性,可以根據這邊的邏輯,修改成自己適合的樣式。

因此特地將這個作者的文章給記錄下來,以防以後連結不見的憾事。

2013年3月25日

解決json_encode中文UNICODE轉碼問題

用 PHP 的 json_encode 來處理中文的時候,中文都會被編碼,變成不可讀的,類似”\u***”的格式,有時候網頁上也會直接顯示null,最主要的原因是為了與前端緊密結合, json 只支援 utf8,如果想要讓中文不進行轉碼,這裡提供兩種簡單方法

1. 在PHP5.4, 這個問題終於得以解決, Json 新增了一個選項: JSON_UNESCAPED_UNICODE, 故名思議, 就是說, Json不要編碼Unicode, 那就不會有中文亂碼的問題了。
echo json_encode("中文", JSON_UNESCAPED_UNICODE);

2. 把中文先 urlencode 然後再使用 json_encode, json_encode 之後再次使用 urldecode 來解碼,這樣編碼出來的 json 中的中文就不會出現 unicode 編碼了。
這裡我提供一個尋訪 array 中 vlaue 為 string 的變數,幫這些變數加上這種機制。

PHP Error Reporting: Turning on Error Reporting in PHP

Modify php.ini

open /etc/php5/apache2/php.ini

ser error_reporting = E_ALL

set display_errors = On

Restart apache server

/etc/init.d/apache2 restart

Check error log

/var/log/apache2/error.log

Reference

2013年3月22日

PHP Time Functions

strtotime

int strtotime ( string $time [, int $now = time() ] )

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

date

string date ( string $format [, int $timestamp = time() ] )

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().