题目是:
假设有123abc456def789这么一个字符串,写一个函数,可以传入一个字符串,和一个要截取的长度。返回截取后的结果。
要求:
1 和标记不得计算在长度之内。
2 截取后的字符串,要保留原有标签,不过如果最后有一个标签没有闭合,则去掉其开始标签。
示例:
题中的字符串,要截取长度5,则返回的字符串应该为:123ab,要截取长度8,应返回123abc45。
5.3 可以把注释去掉{ $i=$j=0; while(strlen($savestring)'); } else { $savestring.=$str[$i]; /*if(strlen($savestring) == $size) { if($str[++$i] =='<') { goto loop; } $i--; }*/ } $i++; } for($j=0; $j<$i; $j++) { $newstring.=$str[$j]; }}function filter($str) // 过滤标签,然后进行比较{ $newstr = ''; $strlength = strlen($str); for($i=0;$i<$strlength;$i++) { switch($str[$i]) { case '' : continue; case ' ' : break; default : $newstr .= $str[$i]; } } return $newstr;}function deletereservedtag(&$string, &$number, $reserved) // 从标签组里去除 保留标签{ $stringlength = count($string); $reservedlength = count($reserved); for($i=0; $i<$stringlength;$i++) // 去掉保留标签部分 { for($j=0;$j < $reservedlength;$j++) { if( filter($string[$i]) == filter($reserved[$j]) ) { $string[$i] = null; for($k=0;$k<2;$k++) { $number[$i][$k] = null; } } } } $tempstring[] = ''; $tempnumber[][] = 0; for($i=0,$j=0; $i < $stringlength; $i++) // 把空的地方填补 { if($string[$i] != null) { $tempstring[$j]=$string[$i]; for($k=0;$k<2;$k++) { $tempnumber[$j][$k] = $number[$i][$k]; } $j++; } } $string = $tempstring; $number = $tempnumber;}function compare(&$string, &$number) //比较成对标签{ $stringlength = count($string); for($i=0; $i < $stringlength; $i++) //删除成对标签 { for($j = $i+1; $j < $stringlength; $j++) { if( filter($string[$i]) == filter($string[$j]) ) { $string[$i] = null; $string[$j] = null; for($k=0;$k<2;$k++) { $number[$i][$k] = null; } for($k=0;$k<2;$k++) { $number[$j][$k] = null; } } } } $tempstring[] = ''; $tempnumber[][] = 0; for($i=0,$j=0; $i < $stringlength; $i++) // 把空的地方填补 { if($string[$i] != null) { $tempstring[$j]=$string[$i]; for($k=0;$k<2;$k++) { $tempnumber[$j][$k] = $number[$i][$k]; } $j++; } } $string = $tempstring; $number = $tempnumber;}function replace(&$string, $number, $newstring){ $tempstring = ''; $savestringlength = strlen($newstring); $stringlength = count($string); //echo $savestringlength.\n.$stringlength; for($i=0, $j=0; $i < $savestringlength; $i++) { if( $j
运算结果如下
------------------------------------------------------------------------------------
截取长度:15
完整长度:123
abc456defg789
处理结果:123
abc456defg78
截取长度:123abc456defg78 (15)
未处理前:123
abc456defg78
回复讨论(解决方案) p4+768mb 机运行速度如下
内存使用情况:2176
算法运行时间(microtime):0.0012378692626953
欢迎各位 php 爱好者来 qq群:23930992
程序处理一些特殊数据的时候会出问题,请把代码粘贴到上面
function filter($str) // 过滤标签,然后进行比较
{
$newstr = '';
$strlength = strlen($str);
for($i=0;$i<$strlength;$i++)
{
switch($str[$i])
{
case '' : continue;
case ' ' : break 2;
default : $newstr .= $str[$i];
}
}
return $newstr;
}
又修正一个错误,请把下面代码粘贴到上面
function replace(&$string, $number, $newstring){ $tempstring = ''; $savestringlength = strlen($newstring); $stringlength = count($string); for($i=0, $j=0; $i < $savestringlength; $i++) { if( $j < $stringlength ) { if( $i == $number[$j][0]) { $begin = $number[$j][1] - $number[$j][0]; while($begin) { $begin--; $i++; } $j++; } } else { $tempstring .= $newstring[$i]; } } $string = $tempstring;}
完整修改后的代码
5.3 可以把注释去掉{ $i=$j=0; while(strlen($savestring)'); } else { $savestring.=$str[$i]; /*if(strlen($savestring) == $size) { if($str[++$i] =='<') { goto loop; } $i--; }*/ } $i++; } for($j=0; $j<$i; $j++) { $newstring.=$str[$j]; }}function filter($str) // 过滤标签,然后进行比较{ $newstr = ''; $strlength = strlen($str); for($i=0;$i<$strlength;$i++) { switch($str[$i]) { case '' : continue; case ' ' : break 2; default : $newstr .= $str[$i]; } } return $newstr;}function deletereservedtag(&$string, &$number, $reserved) // 从标签组里去除 保留标签{ $stringlength = count($string); $reservedlength = count($reserved); for($i=0; $i<$stringlength;$i++) // 去掉保留标签部分 { for($j=0;$j < $reservedlength;$j++) { if( filter($string[$i]) == filter($reserved[$j]) ) { $string[$i] = null; for($k=0;$k<2;$k++) { $number[$i][$k] = null; } } } } $tempstring[] = ''; $tempnumber[][] = 0; for($i=0,$j=0; $i < $stringlength; $i++) // 把空的地方填补 { if($string[$i] != null) { $tempstring[$j]=$string[$i]; for($k=0;$k<2;$k++) { $tempnumber[$j][$k] = $number[$i][$k]; } $j++; } } $string = $tempstring; $number = $tempnumber;}function compare(&$string, &$number) //比较成对标签{ $stringlength = count($string); for($i=0; $i < $stringlength; $i++) //删除成对标签 { for($j = $i+1; $j < $stringlength; $j++) { if( filter($string[$i]) == filter($string[$j]) ) { $string[$i] = null; $string[$j] = null; for($k=0;$k<2;$k++) { $number[$i][$k] = null; } for($k=0;$k<2;$k++) { $number[$j][$k] = null; } } } } $tempstring[] = ''; $tempnumber[][] = 0; for($i=0,$j=0; $i < $stringlength; $i++) // 把空的地方填补 { if($string[$i] != null) { $tempstring[$j]=$string[$i]; for($k=0;$k<2;$k++) { $tempnumber[$j][$k] = $number[$i][$k]; } $j++; } } $string = $tempstring; $number = $tempnumber;}function replace(&$string, $number, $newstring){ $tempstring = ''; $savestringlength = strlen($newstring); $stringlength = count($string); for($i=0, $j=0; $i < $savestringlength; $i++, $j=0) { while( $j
截取长度:160
完整长度:
364867的玉米小店
买家信用:0
卖家好评率:100.00%
买家好评率:0.00%
注册时间:2010年04月29日
荣誉资质:
处理结果:
364867的玉米小店
买家信用:0
卖家好评率:100.00%
买家好评率:0.00%
注册
截取长度:
364867的玉米小店
买家信用:0
卖家好评率:100.00%
买家好评率:0.00%
注册 (160)
未处理前:
364867的玉米小店
买家信用:0
卖家好评率:100.00%
买家好评率:0.00%
注册
内存使用情况:5096
算法运行时间(microtime):0.0040628910064697
精简后的代码
研究得好透彻啊
人都去那了啊,郁闷了
$s = '123abc456def789def789def789def789';function subs($s, $len){//此函数参数有二:$s - 源字符串, $len - 要截取的长度 (当让你也可以将标签传进来,不过要进行其他操作) $s1 = $s; $s1 = preg_replace('//s', '', $s1); $sub = mb_substr($s1, 0, $len, 'gbk'); //处理成数组 $s2 = $s; $s2 = preg_replace('//s', '[p]', $s2); $arr = explode('[p]', $s2); $arr1 = $arr2 = array(); $num = count($arr); $num1 = $num2 = 0; $bs = 0; for($i = 0; $i = $len){ $num1 = $len - $num2 + strlen($arr[$i]); if($num1 != strlen($arr[$i]))$bs = 1 $s3 = mb_substr($arr[$i], 0, $num1, 'gbk'); $arr[$i] = $s3; $arr2 = array_slice($arr, 0, $i + 1); break; } } //处理数组,结合成所需要的成字符串 $num = count($arr2); $s4 = ''; for($i = 0; $i < $num; $i++){ if($i % 2 == 0){ if(($i + 1 == $num && $bs == 1) || ($i + 2 == $num && $bs == 1) || ($num % 2 == 1 && $i + 1 == $num)){ $s4 .= $arr2[$i]; }else{ $s4 .= $arr2[$i].<em>; } }else{ if($i + 1 == $num && $bs == 1){ $s4 .= $arr2[$i]; }else{ $s4 .= $arr2[$i].</em>; } } } //返回结果 return $s4;}$s = subs($s, 17);echo $s;
支持盗版!!!