<code>//--------------------<br />// 基本数据结构<br />//--------------------<br />//二分查找(数组里查找某个元素)<br />function bin_sch($array, $low, $high, $k){<br />if ($low <= $high){<br />$mid = intval(($low+$high)/2);<br />if ($array[$mid] == $k){<br />return $mid;<br />}elseif ($k < $array[$mid]){<br />return bin_sch($array, $low, $mid-1, $k);<br />}else{<br />return bin_sch($array, $mid+1, $high, $k);<br />}<br />}<br />return -1;<br />}<br />//顺序查找(数组里查找某个元素)<br />function seq_sch($array, $n, $k){<br />$array[$n] = $k;<br />for($i=0; $i<$n; $i++){<br />if($array[$i]==$k){<br />break;<br />}<br />}<br />if ($i<$n){<br />return $i;<br />}else{<br />return -1;<br />}<br />}<br />//线性表的删除(数组中实现)<br />function delete_array_element($array, $i)<br />{<br />$len = count($array);<br />for ($j=$i; $j<$len; $j++){<br />$array[$j] = $array[$j+1];<br />}<br />array_pop($array);<br />return $array;<br />}<br />//冒泡排序(数组排序)<br />function bubble_sort($array)<br />{<br />$count = count($array);<br />if ($count <= 0) return false;<br />for($i=0; $i<$count; $i++){<br />for($j=$count-1; $j>$i; $j--){<br />if ($array[$j] < $array[$j-1]){<br />$tmp = $array[$j];<br />$array[$j] = $array[$j-1];<br />$array[$j-1] = $tmp;<br />}<br />}<br />}<br />return $array;<br />}<br />//快速排序(数组排序)<br />function quicksort($array) {<br />if (count($array) <= 1) return $array;<br />$key = $array[0];<br />$left_arr = array();<br />$right_arr = array();<br />for ($i=1; $i<count($array); $i++){<br />if ($array[$i] <= $key)<br />$left_arr[] = $array[$i];<br />else<br />$right_arr[] = $array[$i];<br />}<br />$left_arr = quicksort($left_arr);<br />$right_arr = quicksort($right_arr);<br />return array_merge($left_arr, array($key), $right_arr);<br />}<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 />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 />// 自实现函数<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 />}</code></p>