紅茶小館

一杯紅茶喜相逢 Share Web Tech and Life …
Jul 8
CSS 圖片垂直居中
icon1 vincent | icon2 CSS | icon4 07 8th, 2008| icon337 Comments »

把圖片放在 table 裡, 再把 table id 改成 wrapper 即可。



   html, body, #wrapper {
      height:100%;
      margin: 0;
      text-align: center;
   }
   #wrapper {
      margin: 0 auto;
   }


Jul 8
php text content to utf8
icon1 vincent | icon2 PHP | icon4 07 8th, 2008| icon337 Comments »

一些簡單的步驟即可。

$raw_text = file_get_contents( "http://www.google.com");
$utf8_text = iconv( $encoding, "utf-8", $raw_text );
$utf8_text = html_entity_decode( $utf8_text, ENT_QUOTES, "UTF-8" );
Jul 8
php parse content
icon1 vincent | icon2 PHP | icon4 07 8th, 2008| icon337 Comments »

在 oReilly 的書上看到一個例子, 幾個 function retrieve URL 很有用。

  1. function getURL ($pURL) {
  2.  $_data = null;
  3.  if ($_http = fopen ($pURL, "r")) {
  4.  
  5.   while ( !feof ($_http)) {
  6.    $_data .=fgets ($_http, 1024);
  7.  
  8.   }
  9.   fclose ($_http);
  10.  }
  11.  return ($_data);
  12. }
  13. function cleanString ($pString) {
  14.  $_data = str_replace ( array( chr(10), chr(13), chr(9)), chr(32), $pString);
  15.  while ( strpos( $_data, str_repeat( chr(32), 2), 0) != false) {
  16.   $_data = str_replace ( str_repeat(chr(32),2), chr(32), $_data);
  17.  }
  18.  return trim($_data);
  19. }
  20. function getBlock ($pStart, $pStop, $pSource, $pPrefix = true) {
  21.  $_data = null;
  22.  $_start = strpos(strtolower($pSource), strtolower($pStart),0);
  23.  $_start = ( $pPrefix ==false) ? $_start + strlen($pStart):$_start;
  24.  $_stop = strpos ( strtolower ($pSource), strtolower ($pStop), $_start);
  25.  if ($_start > strlen($pelement) && $_stop >$_start ) {
  26.   $_data = trim( substr( $pSource, $_start, $_stop - $_start ));
  27.  }
  28.  return ($_data);
  29. }
  30.  
  31. function getElement($pElement, $pSource) {
  32.  $_data = null;
  33.  $pElement = strtolower($pElement);
  34.  $_start=strpos(strtolower($pSource), chr(60) . $pElement, 0);
  35.  $_start=strpos($pSource, chr(62), $_start ) +1;
  36.  $_stop = strpos ( strtolower($pSource), "<!–" . $pElement . chr(62), $_start );
  37.  if ($_start–>strlen($pElement) &amp;&amp; $_stop &gt; $_start) {
  38.    $_data = trim (substr($pSource, $_start, $_stop - $_start));
  39.  
  40.  }
  41.  return ($_data);
  42. }
Jul 4
PHP plain text to html
icon1 vincent | icon2 PHP | icon4 07 4th, 2008| icon3No Comments »

工作需要把一個 folder 裡的 text file 轉成 html

這個網站有sample code, 只要寫個function read directory,

把 filename 放到 array 裡即可。

Jul 2

另一個手工打造的 Jquery 例子.較輕巧方便。

http://15daysofjquery.com/edit-in-place-with-ajax-using-jquery-javascript-library/15/

Jul 2
Jquery edit in place
icon1 vincent | icon2 JavaScript | icon4 07 2nd, 2008| icon31 Comment »

Project 需要做一個 in place edit 的表格,

JQuery 已有plug-in, 相當好用. 不用自己寫 . good~

http://blog.yoren.info/tag/edit-in-place/

Jul 2
ajax sample code
icon1 vincent | icon2 JavaScript | icon4 07 2nd, 2008| icon31 Comment »

每個 Event 都有手工打造的 ajax 例子, 可以下載 source code

研究. 用了mvc model.

http://www.studiosedition.com/?events=

Jun 28
flash photo slide banner
icon1 vincent | icon2 Flash | icon4 06 28th, 2008| icon3No Comments »

今天應朋友之約跟書的例子做了一個簡單的 相反移動的 photo slide,
DEMO

圖片都是hard code 在 flash 中. 其實可以做 xml load external photo.

大家可自行研究 source code 改進。

Source File

Jun 27
xml and xsl
icon1 vincent | icon2 JavaScript | icon4 06 27th, 2008| icon3No Comments »

由於公司的  SERVER 不支援 SERVER SIDE programming 和沒有 DB .

要做Data searching and filtering 唯有借助 xml,
用 xsl接收不同的參數做不同的模版,
再用 javascript 把 xml transform 成 HTML
詳細看了這個網站的 例子和 source code, 終於成功。
http://msell.customer.netspace.net.au/

Jun 26
PHP magic_quotes
icon1 vincent | icon2 PHP | icon4 06 26th, 2008| icon3No Comments »

做 backend DB 常常要處理 single quote, double quote 在 sql 裡的問題,
尤其是php 不知server 是否 開啟了 get_magic_quotes_gpc() 設定,
所以用addslashes並不是好方法.
在網上找到這個處理方法, 很不錯,或者用 str_replace把
single quote, double quote 變成 htmlentities。

if(!get_magic_quotes_gpc())
{
  $_GET = array_map('mysql_real_escape_string', $_GET);
  $_POST = array_map('mysql_real_escape_string', $_POST);
  $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}else
{
   $_GET = array_map('stripslashes', $_GET);
   $_POST = array_map('stripslashes', $_POST);
   $_COOKIE = array_map('stripslashes', $_COOKIE);
   $_GET = array_map('mysql_real_escape_string', $_GET);
   $_POST = array_map('mysql_real_escape_string', $_POST);
   $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
Jun 26
PHP parse CSV
icon1 vincent | icon2 PHP | icon4 06 26th, 2008| icon337 Comments »

用 PHP 內置  function filegetcsv 去 parse CSV file, 常有 big5, utf8 亂碼問題。
最後還是用原始方法, 逐行 read, 再用 explode 變成2d array. 才解決到問題。

$dataArr = file(data.txt);
  for ($i=0;$i<count($dataArr);$i++){
     $line = explode("~", $dataArr[$i]);
     foreach($line as $value) {
        $finalData[$i][] = $value;
    }
}

« Previous Entries Next Entries »