例子 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"; ?>
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"; ?>
Description
string mb_ereg_replace ( string pattern, string replacement, string string [, array option])This function is supported in PHP 4.2.0 or higher.
Fatal error: Call to undefined function: mbereg_replace() in d:\usr\www\html\test2.php on line 4
$fontlib="至";
$post="分裂";
echo str_replace($fontlib,"c",$post);
===========================================
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);
while($str)
修改为
while(strlen($str))
$fontlib=array("至","分");
$fontc=array("a","c");
$post="分裂";
echo m_replace($fontlib,$fontc,$post);
这样就不行了。
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);
}
仅仅[\x80-\xff]不准确
$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";
?>
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";
?>
我要把$post里面的$fontlib字符替换成$fontb,楼上的内容没用阿。
string mb_ereg_replace ( string pattern, string replacement, string string [, array option])