<code><?<br />//------------------------<br />// PHP内置字符串函数实现<br />//------------------------<br />//字符串长度<br />function strlen ($str)<br />{<br /> if ($str == '' ) return 0;<br /> $count = 0;<br /> while (1){<br /> if ( $str[$count] != NULL){<br /> $count++;<br /> continue;<br /> }else{<br /> break;<br /> }<br /> }<br /> return $count;<br />}<br />//截取子串<br />function substr($str, $start, $length=NULL)<br />{<br /> if ($str== '' || $start>strlen($str )) return;<br /> if (($length!=NULL) && ( $start>0) && ($length> strlen($str)-$start)) return;<br /> if (( $length!=NULL) && ($start< 0) && ($length>strlen($str )+$start)) return;<br /> if ($length == NULL) $length = (strlen($str ) - $start);<br /> <br /> if ($start < 0){<br /> for ($i=(strlen( $str)+$start); $i<(strlen ($str)+$start+$length ); $i++) {<br /> $substr .= $str[$i];<br /> }<br /> }<br /> if ($length > 0){<br /> for ($i= $start; $i<($start+$length ); $i++) {<br /> $substr .= $str[$i];<br /> }<br /> }<br /> if ( $length < 0){<br /> for ($i =$start; $i<(strlen( $str)+$length); $i++) {<br /> $substr .= $str[$i ];<br /> }<br /> }<br /> return $substr;<br />}<br />//字符串翻转<br />function strrev($str)<br />{<br /> if ($str == '') return 0 ;<br /> for ($i=(strlen($str)- 1); $i>=0; $i --){<br /> $rev_str .= $str[$i ];<br /> }<br /> return $rev_str;<br />}<br />//字符串比较<br />function strcmp($s1, $s2)<br />{<br /> if (strlen($s1) < strlen($s2)) return -1 ;<br /> if (strlen($s1) > strlen( $s2)) return 1;<br /> for ($i =0; $i<strlen($s1 ); $i++){<br /> if ($s1[ $i] == $s2[$i]){<br /> continue;<br /> }else{<br /> return false;<br /> }<br /> }<br /> return 0;<br />}<br />//查找字符串<br />function strstr($str, $substr)<br />{<br /> $m = strlen($str);<br /> $n = strlen($substr );<br /> if ($m < $n) return false ;<br /> for ($i=0; $i <=($m-$n+1); $i ++){<br /> $sub = substr( $str, $i, $n);<br /> if ( strcmp($sub, $substr) == 0) return $i;<br /> }<br /> return false ;<br />}<br />//字符串替换<br />function str_replace($substr , $newsubstr, $str)<br />{<br /> $m = strlen($str);<br /> $n = strlen($substr );<br /> $x = strlen($newsubstr );<br /> if (strchr($str, $substr ) == false) return false;<br /> for ( $i=0; $i<=($m- $n+1); $i++){<br /> $i = strchr($str, $substr);<br /> $str = str_delete ($str, $i, $n);<br /> $str = str_insert($str, $i, $newstr);<br /> }<br /> return $str ;<br />}<br />?><br /><?php<br />//--------------------<br />// 自实现字符串处理函数<br />//--------------------<br />//插入一段字符串<br />function str_insert($str, $i , $substr)<br />{<br /> for($j=0 ; $j<$i; $j ++){<br /> $startstr .= $str[$j ];<br /> }<br /> for ($j=$i; $j <strlen($str); $j ++){<br /> $laststr .= $str[$j ];<br /> }<br /> $str = ($startstr . $substr . $laststr);<br /> return $str ;<br />}<br />//删除一段字符串<br />function str_delete($str , $i, $j)<br />{<br /> for ( $c=0; $c<$i; $c++){<br /> $startstr .= $str [$c];<br /> }<br /> for ($c=( $i+$j); $c<strlen ($str); $c++){<br /> $laststr .= $str[$c];<br /> }<br /> $str = ($startstr . $laststr );<br /> return $str;<br />}<br />//复制字符串<br />function strcpy($s1, $s2 )<br />{<br /> if (strlen($s1)==NULL || !isset( $s2)) return;<br /> for ($i=0 ; $i<strlen($s1); $i++){<br /> $s2[] = $s1 [$i];<br /> }<br /> return $s2;<br />}<br />//连接字符串<br />function strcat($s1 , $s2)<br />{<br /> if (!isset($s1) || !isset( $s2)) return;<br /> $newstr = $s1 ;<br /> for($i=0; $i <count($s); $i ++){<br /> $newstr .= $st[$i ];<br /> }<br /> return $newsstr;<br />}<br />//简单编码函数(与php_decode函数对应)<br />function php_encode($str)<br />{<br /> if ( $str=='' && strlen( $str)>128) return false;<br /> for( $i=0; $i<strlen ($str); $i++){<br /> $c = ord($str[$i ]);<br /> if ($c>31 && $c <107) $c += 20 ;<br /> if ($c>106 && $c <127) $c -= 75 ;<br /> $word = chr($c );<br /> $s .= $word;<br /> } <br /> return $s; <br />}<br />//简单解码函数(与php_encode函数对应)<br />function php_decode($str)<br />{<br /> if ( $str=='' && strlen($str )>128) return false;<br /> for( $i=0; $i<strlen ($str); $i++){<br /> $c = ord($word);<br /> if ( $c>106 && $c<127 ) $c = $c-20;<br /> if ($c>31 && $c< 107) $c = $c+75 ;<br /> $word = chr( $c);<br /> $s .= $word ;<br /> } <br /> return $s; <br />}<br />//简单加密函数(与php_decrypt函数对应)<br />function php_encrypt($str)<br />{<br /> $encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567890';<br /> $decrypt_key = 'ngzqtcobmuhelkpdawxfyivrsj2468021359';<br /> if ( strlen($str) == 0) return false;<br /> for ($i=0; $i<strlen($str); $i ++){<br /> for ($j=0; $j <strlen($encrypt_key); $j ++){<br /> if ($str[$i] == $encrypt_key [$j]){<br /> $enstr .= $decrypt_key[$j];<br /> break;<br /> }<br /> }<br /> }<br /> return $enstr;<br />}<br />//简单解密函数(与php_encrypt函数对应)<br />function php_decrypt($str)<br />{<br /> $encrypt_key = 'abcdefghijklmnopqrstuvwxyz1234567890';<br /> $decrypt_key = 'ngzqtcobmuhelkpdawxfyivrsj2468021359';<br /> if ( strlen($str) == 0) return false;<br /> for ($i=0; $i<strlen($str); $i ++){<br /> for ($j=0; $j <strlen($decrypt_key); $j ++){<br /> if ($str[$i] == $decrypt_key [$j]){<br /> $enstr .= $encrypt_key[$j];<br /> break;<br /> }<br /> }<br /> }<br /> return $enstr;<br />}<br />?></code></p>