时间:2021-07-01 10:21:17 帮助过:9人阅读
 5.3 可以把注释去掉{    $i=$j=0;    while(strlen($saveString)<$size)    {        //loop:        if($str[$i] == '<')        {            $k=0;            $number[$j][$k++]=$i;            $string[$j]='';            do            {                $string[$j].=$str[$i];                if($str[$i] == '>')                {                    $number[$j++][$k++]=$i;                    break;                }            }while($str[$i++] != '>');        }        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 '/' : continue;            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 < $stringLength )        {            if( $i == $number[$j][0])            {                $begin = $number[$j][1] - $number[$j][0];                while($begin)                {                    $begin--;                    $i++;                }               $j++;               $i++;            }        }       $tempString .= $newString[$i];    }    $string = $tempString;}$str = '123
abc45
6defg789
';$size=15;$reserved = array //保留标签(    '
',    '
',    '
',    '
');$string[]='';                                                  // 保存所有标签$number[][]=0;                                                 // 保存所有标签位置$saveString = '';                                              // 保存截取标签后的所有字符串$newString  = '';                                              // 保存所有未截取标签的字符串find($str, $size, $string, $number, $saveString, $newString);  // 查找标签deleteReservedTag($string, $number, $reserved);                // 从标签组里去除 保留标签compare($string, $number);                                     // 从标签组里去除 成对标签replace($string, $number, $newString);printf("截取长度:%d\n\n", $size);printf("完整长度:%s\n\n",$str);printf("处理结果:%s\n\n", $string);printf("截取长度:%s (%s)\n\n",$saveString,strlen($saveString));printf("未处理前:%s\n",$newString);?>  123
abc45
6defg789
123
abc45
6defg78  
  
 
  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 '/' : continue;  
             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)<$size)    {        //loop:        if($str[$i] == '<')        {            $k=0;            $number[$j][$k++]=$i;            $string[$j]='';            do            {                $string[$j].=$str[$i];                if($str[$i] == '>')                {                    $number[$j++][$k++]=$i;                    break;                }            }while($str[$i++] != '>');        }        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 '/' : continue;            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 < $stringLength )        {            if( $i == $number[$j][0])            {                $i = $number[$j][1];                $i++;            }            $j++;        }        $tempString .= $newString[$i];    }    $string = $tempString;}$sttime = microtime(true);$stmem = memory_get_usage();$str = '        |  | 364867的玉米小店买家信用:0卖家好评率:100.00% 买家好评率:0.00% 注册时间:2010年04月29日 荣誉资质:  | 
|  | 364867的玉米小店买家信用:0 卖家好评率:100.00% 买家好评率:0.00% 注册时间:2010年04月29日 荣誉资质:  | 

|  | 364867的玉米小店买家信用:0 卖家好评率:100.00% 买家好评率:0.00% 注册 内存使用情况:5096 算法运行时间(microtime):0.0040628910064697   精简后的代码   ');            $number[$j++][$k]=$i;        }        else        {            $saveString.=$str[$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 '/' : continue;            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;                }            }        }    }}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] = $string[$j] = NULL;                for($k=0;$k<2;$k++)                {                    $number[$i][$k] = $number[$j][$k] = NULL;                }            }        }    }        //begin 把标签组空的地方 填补 (你可以去除本小段代码,代价是运行时间增加了)    $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;    //end}function replace(&$string, $number, $newString) // 核心代码 ...{    $tempString       = '';    $saveStringLength = strlen($newString);    $stringLength     = count($string);    for($i=0, $j=0; $i < $saveStringLength; $i++, $j=0)    {        while( $j < $stringLength )        {            if( $i == $number[$j][0])            {                $i = $number[$j][1];                $i++;            }            $j++;        }        $tempString .= $newString[$i];    }    $string = $tempString;}$sttime = microtime(true);$stmem = memory_get_usage();$str = '研究得好透彻啊 人都去那了啊,郁闷了 
 $s = '123abc456def789def789def789def789';function subs($s, $len){//此函数参数有二:$s - 源字符串,    $len - 要截取的长度        (当让你也可以将标签传进来,不过要进行其他操作)    $s1 = $s;    $s1 = preg_replace('/<\/?em>/s', '', $s1);    $sub = mb_substr($s1, 0, $len, 'gbk');    //处理成数组    $s2 = $s;    $s2 = preg_replace('/<\/?em>/s', '[p]', $s2);    $arr = explode('[p]', $s2);    $arr1 = $arr2 = array();    $num = count($arr);    $num1 = $num2 = 0;    $bs = 0;    for($i = 0; $i < $num; $i++){        $arr1[] = strlen($arr[$i]);        $num2 = array_sum($arr1);        if($num2 >= $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;支持盗版!!! 人气教程排行
 |