PHP Classes

A fix to your code

Recommend this page to a friend!

      Chinese Character Conversion  >  All threads  >  A fix to your code  >  (Un) Subscribe thread alerts  
Subject:A fix to your code
Summary:it had some bugs, so here is my code
Messages:1
Author:Peter Kahl
Date:2011-04-25 22:21:48
 

  1. A fix to your code   Reply   Report abuse  
Picture of Peter Kahl Peter Kahl - 2011-04-25 22:21:49
I enjoyed very much! Unfortunately, it is as slow as my existing method.

But found bugs in your code and fixed it:

private function tr($str, $_fr, $_to) {
$len = mb_strlen($str, 'utf-8');
$i = 0;
$new = '';
while($i < $len) {
$char = mb_substr($str, $i, 1, 'utf-8');
$pos = mb_strpos($_fr, $char, 0, 'utf-8');
if ($pos !== false) {
$new .= mb_substr($_to, $pos, 1, 'utf-8');
}
else {
$new .= $char;
}
$i++;
}
return $new;
}

function simp2trad($str) {
return CConvert::tr($str, CConvert::simp, CConvert::trad);
}

function trad2simp($str) {
return CConvert::tr($str, CConvert::trad, CConvert::simp);
}