帮忙看看以下代码,为何直接调用doConvert就可以得到正确结果,其他函数就得不到正确结果?
<?php
include_once("function/bg.inc");
include_once("function/gb.inc");
//通过查表实现繁体与简体之间的互换
function doConvert($s, &$search, &$replace) {
$newstring="";
$charset="utf-8";
for($i=0;$i <mb_strlen($s, $charset);$i++) {
$char=mb_substr($s, $i, 1, $charset);
$len=strlen($char);
if($len>1) {
$pos=strpos($search, $char);
echo "char=$char, len=$len, pos=$pos <br/>";
if($pos)
$char=substr($replace, $pos, $len);
}
$newstring = $newstring . $char;
}
return $newstring;
}
//转换为繁体
function toTC($s) {
return doConvert($s, $gb, $bg);
}
//转换为简体
function toSC($s) {
return doConvert($s, $bg, $gb);
}
//依据指定语言转换
function strConvert($s, $ch) {
if($ch=="sc")
return toTC($s);
else
return toSC($s);
}
$s1="zhong华ren民共和国";
$s2="職能技術";
$newstring=doConvert($s1, $gb, $bg);
echo "$s1=>$newstring <br/>";
$newstring=toTC($s1, $gb, $bg);
echo "$s1=>$newstring <br/>";
$newstring=strConvert($s1, "tc");
echo "$s1=>$newstring <br/>";
?>
输出结果:
char=华, len=3, pos=1299
char=民, len=3, pos=
char=共, len=3, pos=
char=和, len=3, pos=
char=国, len=3, pos=654
zhong华ren民共和国=>zhong華ren民共和國(doConvert正确)
char=华, len=3, pos=
char=民, len=3, pos=
char=共, len=3, pos=
char=和, len=3, pos=
char=国, len=3, pos=
zhong华ren民共和国=>zhong华ren民共和国(toTC错误)
char=华, len=3, pos=
char=民, len=3, pos=
char=共, len=3, pos=
char=和, len=3, pos=
char=国, len=3, pos=
zhong华ren民共和国=>zhong华ren民共和国(strConvert错误)