echo str_replace($fontlib,"$post",$post);

解决方案 »

  1.   

    echo mbereg_replace($fontlib,"c",$post);
      

  2.   

    没有查到mbereg_replace函数的详细说明。这个函数要php5才能支持?
      

  3.   

    函数参考->Multi-Byte String Functionsmb_ereg_replace(4.2.0 - 4.3.2 only)mb_ereg_replace -- Replace regular expression with multibyte support
    Description
    string mb_ereg_replace ( string pattern, string replacement, string string [, array option])This function is supported in PHP 4.2.0 or higher.
      

  4.   

    查看php版本的那个函数是什么来的?PHP_INFO()?
      

  5.   

    我在我的php4.3.4里运行,提示:
    Fatal error: Call to undefined function: mbereg_replace() in d:\usr\www\html\test2.php on line 4
      

  6.   

    要加载mbstring模块,怎么都不看手册?
      

  7.   

    这是个很简单的问题:汉字是双字节的。你硬要拆开一个汉字,编码自然会变化的。===========================================
    $fontlib="至";
    $post="分裂";
    echo str_replace($fontlib,"c",$post);
    ===========================================
      

  8.   

    刚写了一个函数,欢迎大家多测试:
    function m_replace($search,$replace,$str)
    {
    $len = strlen($search);
    $r = "";
    while($str){
    $k = 1;
    if(substr($str,0,$len)==$search){
    $r .= $replace;
    $str = substr($str,$len);
    }else{
    if(ord(substr($str,0,1))>128&&ord(substr($str,1,1))>=64&&ord(substr($str,1,1))!=127) $k=2;
    $r .= substr($str,0,$k);
    $str = substr($str,$k);
    }
    }
    return $r;
    }
    $fontlib="至";
    $post="分裂";
    echo m_replace($fontlib,"c",$post);
      

  9.   

    补充:
    while($str)
    修改为
    while(strlen($str))
      

  10.   

    没法等同于str_replace,没法替换数组。怎么办?总不能放到循环里面替换吧?那样效率太低了。
    $fontlib=array("至","分");
    $fontc=array("a","c");
    $post="分裂";
    echo m_replace($fontlib,$fontc,$post);
    这样就不行了。
      

  11.   

    我靠,我的帖子怎么没有人回,都集中在这里了!!!即然大家都这么冷落我,那我就索性给楼主一个惊喜!!!!!高效的中文字符串截取函数
    function c_substr($str,$start=0) {
      $ch = chr(127);
      $p = array("/[\x81-\xfe]([\x81-\xfe]|[\x40-\xfe])/","/[\x01-\x77]/");
      $r = array("","");
      if(func_num_args() > 2)
        $end = func_get_arg(2);
      else
        $end = strlen($str);
      if($start < 0)
        $start += $end;  if($start > 0) {
        $s = substr($str,0,$start);
        if($s[strlen($s)-1] > $ch) {
          $s = preg_replace($p,$r,$s);
        $start += strlen($s);
        }
      }
      $s = substr($str,$start,$end);
      $end = strlen($s);
      if($s[$end-1] > $ch) {
        $s = preg_replace($p,$r,$s);
        $end += strlen($s);
      }
      return substr($str,$start,$end);
    }function m_substr($str,$start) {
      preg_match_all("/[\x80-\xff]?./",$str,$ar);
      if(func_num_args() >= 3) {
        $end = func_get_arg(2);
        return join("",array_slice($ar[0],$start,$end));
      }else
        return join("",array_slice($ar[0],$start));
    }性能测试:
    1、使用pear的Bench_Iterate类作为计时器
    2、以循环判断的对照函数
    function TrimChinese($str,$len){
      $r_str="";
      $i=0;
      while ($i<$len){
        $ch=substr($str,$i,1);
        if(ord($ch)>0x80) $i++;
          $i++;
      }
      $r_str=substr($str,0,$i);
      return $r_str;
    }
    3、测试环境:p2/166、nt4 iis4+php4.3.1
    4、测试代码:
    require_once "Bench/Iterate.php";
    $bench = new Bench_Iterate;$bench->run(100, "TrimChinese", $str , 1000);
    $result = $bench->get();
    echo "TrimChinese:".$result[mean]."<br>";$bench->run(100, "c_substr", $str , 3,1000);
    $result = $bench->get();
    echo "c_substr:".$result[mean]."<br>";$bench->run(100, "m_substr", $str , 3,1000);
    $result = $bench->get();
    echo "m_substr:".$result[mean]."<br>";$bench->run(100, "mb_substr", $str , 3,1000);
    $result = $bench->get();
    echo "mb_substr:".$result[mean]."<br>";
    5、测试文字:本文
    6、测试结果:
    TrimChinese:0.058972
    c_substr:0.000809
    m_substr:0.000666
    mb_substr:0.000458附:
    function m_strlen($str) {
      preg_match_all("/[\x80-\xff]?./",$str,$ar);
      return count($ar[0]);
    }function m_chunk_split($str,$size=64) {
      preg_match_all("/[\x80-\xff]?./",$str,$ar);
      $a = array_chunk($ar[0],$size);
      foreach($a as $k=>$v)
        $a[$k] = join("",$a[$k]); 
      return join("\r\n",$a);
    }
      

  12.   

    TO 楼上:
    仅仅[\x80-\xff]不准确
      

  13.   

    例子 1. substr_replace() example<?php
    $var = '中国';
    echo "Original: $var<hr />\n";/* These two examples replace all of $var with 'bob'. */
    echo substr_replace($var, '台湾', 0) . "<br />\n";
    echo substr_replace($var, '台湾', 0, strlen($var)) . "<br />\n";
    ?>  
      

  14.   

    Description
    string substr_replace ( string string, string replacement, int start [, int length])
    substr_replace() replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement. The result is returned. If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string. If length is given and is positive, it represents the length of the portion of string which is to be replaced. If it is negative, it represents the number of characters from the end of string at which to stop replacing. If it is not given, then it will default to strlen( string ); i.e. end the replacing at the end of string. <?php
    $fontlib="至";
    echo "$fontlib<hr />\n";
    $post="分裂";/* These two examples replace all of $fontlib with '分裂'. */
    echo substr_replace($fontlib, '$post, 0) . "<br />\n";
    echo substr_replace($fontlib, '分裂', 0, strlen($fontlib)) . "<br />\n";
    ?>
      

  15.   

    恕我才疏学浅,看不懂楼上贴的东西和我的替换有什么关系。
    我要把$post里面的$fontlib字符替换成$fontb,楼上的内容没用阿。
      

  16.   

    试试这个有没用吧.
    string mb_ereg_replace ( string pattern, string replacement, string string [, array option])
      

  17.   

    我是要文字替换,但是所有的文字都是数组组成的。怎么替换?至于上面那个函数,提示说Call to undefined function: mb_ereg_replace() 。这个函数需要特殊模块,好像一般的服务器默认都不支持。