2010年5月25日 星期二

Some notes for symfony

View Template Shortcuts

  • $sf_context: The whole context object (instance of sfContext)
  • $sf_request: The request object (instance of sfRequest) 
  • $sf_params: Parameters of the request 
  • $sf_user: The current user session object (instance of sfUser)

      Get XML information in symfony 1.4

      When parse a remote xml file, I use sfWebBrowserPlugin and get the right result, like this way:

      in actions code
      $tar = new sfWebBrowser();
      $tar->post(url_for('MsgTransfer/Recommender', true), http_build_query($this->form->getValues()));
      $this->xml = $tar->getResponseXML(); 
      
      in template code
      php echo $xml->string;
      then i get the error message:
      Fatal error: Call to undefined method SimpleXMLElement::__toString() in /usr/share/php/symfony/escaper/sfOutputEscaperObjectDecorator.class.php on line 98

      The solution to solve is use:
      php echo $sf_data->getRaw('xml')->string;
      

      2010年5月19日 星期三

      UNIX 終端機色彩控制碼

      可以用於各種程式碼的輸出資訊顯示,包含C/C++、PHP、SHELL等,

      以PHP為例:
      echo "\033[40;31mHello!\n\033[0m";
      以C為例:
      printf("\033[40;31mHello!\n\033[0m");

      標準格式:
      \033[背景色碼;文字色碼m String here \033[0m

      背景色碼範圍:40~47
      • 40:黑
      • 41:深紅
      • 42:綠
      • 43:黃
      • 44:藍
      • 45:紫
      • 46:深綠
      • 47:白
      文字色碼範圍:30~37
      • 30:黑
      • 31:紅
      • 32:綠
      • 33:黃
      • 34:藍
      • 35:紫
      • 36:深綠
      • 37:白
      特效屬性控制碼:
      • \33[0m:關閉所有屬性
      • \33[1m:設置高亮度
      • \33[4m:設置下底線
      • \33[5m:文字閃爍
      • \33[7m:反顯示(開燈)
      • \33[8m:消隱
      更詳細資訊可參考:教您如何在BBS介面上利用色碼上色 from NCTU-Wiki

      2010年5月4日 星期二

      PHP: How to convert ISO character (HTMLEntities) to UTF-8?

      最近在寫針對 Amazon.com Bookstore 的 HTML Crawler 程式,

      發現原來 Amazon.com 的網頁編碼居然不完全都是 UTF-8 編碼,

      讓我抓下資料(ISO編碼)後,存不進預設編碼為 UTF-8 的資料庫,

      於是乎,我向咕狗大神詢問了一番,獲得了以下的解答:

      I’m facing problem in how to convert string from ISO to UTF8 previously. Due to the server configuration problem, all the UTF-8 char has been convereted into ISO (HTMLEntities) before it insert into db and those ISO character (HTMLEntities) break while showing in XML document. Now i found the solution to convert ISO character into UTF-8.

      The original utf-8 chinese words: "你好"
      Converted to ISO (HTMLEntities) : "& #20320;& #22909;"

      For you to convert the ISO string "& #20320;& #22909;" to become utf-8 chinese words: "你好", you need to use Multibyte String function (or mbstring extension). Example below uses mb_convert_encoding function to convert ISO (HTMLEntities) characters to UTF-8.

      ?php $str =  "& #20320;& #22909;";
      echo mb_convert_encoding($str, 'UTF-8', 'HTML-ENTITIES'); ?