Wednesday, October 27, 2010

Nginx rewrite rule for Kohana

This took me a while to investigate how to set the rewrite rule for Kohana in nginx, then I found it is really easy

location / {    root   c:/web;    index  index.php index.html index.htm;    if (-f $request_filename){        break;    }    if (-d $request_filename){        break;    }    rewrite ^/ANY_PATH/(.+)$ /ANY_PATH/index.php?kohana_uri=$1 last;}

Thursday, October 14, 2010

PHP shell - interactive mode

$ php -aInteractive shellphp > echo time();1287046854

Tested ok with Linux, yet Windows seems has some problem with it

Monday, October 11, 2010

Google toolbar doesn't support Google Chrome. That's funny

First of all, I’m a fan of Google so this article is not going to talk approximately any thing sucks, it is just I found this comical issue that google tool bar is only support Internet Explorer & Firefox at moment. It is a bit weird that is not officially support Google’s own product – Google Chrome as I have never heard of Microsoft Live tool bar doesnt officially support Internet Expolorer before.

Well, I laughted,  this is really comical I think, I hope Google will correct this ASAP 😀

Tuesday, October 5, 2010

Use DOMDocument to parse non utf-8 encoding web page in PHP

Recently I was digging around in PHP + curl + DOMDocument, there are quite lot of impressive facilities such as DOMxPath, curl post, cookies, it is very effortless to simulate any action on an website without JavaScript depend. Here is some problem & tricks I found when I handle any non utf-8 encoding with CURL & DOMDocument.

Case 1:
Parsing a non utf-8 encoding page to DomDocument, Some web page put tag in following sequence

<html><head><title>NON UTF-8 TITLE</title><meta http-equiv="Content-Type" content="text/html; charset=ENCODING"/>

Assuming you have just received the html content from curl_exec

//....$htmlContent = curl_exec($ch);$doc=new DocDocument('1.0', 'ENCODING'); //create a new DOMDocument object$doc->loadHtml($htmlContent); //you probably obtain warning here$doc->save('test.html');

Open your test.html with any text editor, you may find the your html body is gone & the header is incomplete.

To resolve this problem, you will have to put the title after the

Here is a simple trick to do

$htmlContent = curl_exec($ch);$pattern="/(<title>.*<\/title>)[.\s]*(<meta\s*http-equiv=\"Content-Type\"\s*content=\"text\/html; charset=gb2312\"\s*\/>)/i";$htmlContent=preg_replace($pattern,"$2\r\n$1",$htmlContent);$doc=new DocDocument('1.0', 'ENCODING');$doc->loadHtml($htmlContent);

Now you should obtain the proper document content without lose anything.