2009/08/04

PHP 處理許功蓋問題的 Function

(撰寫於 2006-12-08 )

本函式轉載自 PHP + MySQL 程式設計研習
這個作者的網站是我目前看過,最淺顯易懂,也最實用的

目前這個函式,在 MySQL 各個版本使用上都沒問題
當然,MySQL 4.1 之後和 4.1 之前的版本,在 Big5 Coding 環境的設定上
有所不同,請看此篇

function Fix_Backslash($org_str)
{
// ----------------------------------------
// mysql_client_encoding() 這個 PHP 函數目前是有 bug 的 (PHP version < 5, 2006-12)
// 它總是回傳 latin1,而無法正確得知 mysql 的 character-set
// 雖然無法釐清是 PHP 還是 MySQL 的責任
// 不過在 MySQL 官網的討論區已經有人提出說明了:
//
// http://forums.mysql.com/read.php?103,14397,25128#msg-25128
// http://forums.mysql.com/read.php?52,56632,57821#msg-57821
//
// 所以這裡的函數我略加修正
// ----------------------------------------

// if ( mysql_client_encoding() != "big5" ) return $org_str;

$tmp_length = strlen($org_str);

for ( $tmp_i=0; $tmp_i<$tmp_length; $tmp_i++ )
{
$ascii_str_a = substr($org_str, $tmp_i , 1);
$ascii_str_b = substr($org_str, $tmp_i+1, 1);

$ascii_value_a = ord($ascii_str_a);
$ascii_value_b = ord($ascii_str_b);

if ( $ascii_value_a > 128 )
{
if ( $ascii_value_b == 92 )
{
$org_str = substr($org_str, 0, $tmp_i+2) . substr($org_str,$tmp_i+3);
$tmp_length = strlen($org_str);
}
$tmp_i++;
}
}

$tmp_length = strlen($org_str);
if ( substr($org_str, ($tmp_length-1), 1) == "\\" ) $org_str .= chr(32);

$org_str = str_replace("\\0", "\ 0", $org_str);
return $org_str;
}
// 本函式轉載自 http://chensh.loxa.edu.tw/php/X_1.php
?>

沒有留言:

張貼留言

Web 技術中的 Session 是什麼?

轉載原文:  Web 技術中的 Session 是什麼? 本文目標 在 Web 的世界裡,Session 已經是被廣泛使用的一種技術,大多數的 Web 開發者,肯定都能運用自如。在現今的各類 Web 應用程式的開發框架和工具中,Session 也已經被包裝成容易使用的模...