Sunday, December 5, 2010

研究一下Unicode﹙解決PHP5,中文字插入中文句子問題﹚


<?php

$newstring=substr_replace('歡今宵', '樂', 1, 0);

echo $newstring; // ?樂??今宵 // 出錯
echo '<BR/>';


function insertString ( $s, $r, $w ) {

// ---------- insert the string after the $w-th character
// ---------- Chinese characters can be used in this function

// $s is the whole string
// $r is the string to be inserted into $s
// $w is the position after $w-th character. $r is to be inserted at this position.

// ---- examples below ----
// echo insertString ( '功蓋分國', '三', 2 ) ;
// gives 功蓋三分國
// echo insertString ( '房地買賣', '短期', 0 ) ;
// gives 短期房地買賣
// echo insertString ( '氣溫回升一到度', '兩', 6 ) ;
// gives 氣溫回升一到兩度
// echo insertString ( '國家級文化藝術名家的書法、攝影', '、水墨', 12 ) ;
// gives 國家級文化藝術名家的書法、水墨、攝影
// ---- examples above ----
// avoid an error below
if ( ( mb_strlen ($s, 'UTF-8') == 0 ) | ( mb_strlen ($r, 'UTF-8') == 0 ) ) {
// do _not_ allow null input
exit;
}

$t = mb_strlen ($s, 'UTF-8');

// avoid an error below
if ( $w > $t ) {
// $w is more than the total number of characters

exit;
}
if ( $w > 0 ) {
$u = mb_substr($s, 0, $w, 'UTF-8');
// get the characters up to the $w-th character

$n = mb_strlen ( $u, 'latin1' );
// get the number of bytes up to the $w-th character

$v = preg_replace( '/(.{' . $n . '})(.*)/', "$1$r$2", $s);

// A period (".") represents "any character except the new line character"
// The asterisk (*) represents zero or more characters

} // if ( $w > 0 ) {

if ( $w == 0 ) {
$v = $r . $s;
}

return $v;
} // function insertString ( $s, $r, $w ) {

echo insertString ( '氣溫回升一到度', '兩', 6 ) ;
echo '<BR/>';
echo insertString ( '國家級文化藝術名家的書法、攝影', '、水墨', 12 ) ;
echo '<BR/>';
echo insertString ( '功蓋分國', '三', 2 ) ;
echo '<BR/>';
echo insertString ( '房地買賣', '短期', 0 ) ;



?>

No comments:

RSS