快速排序
function quick_sort($array) {
if (count($array) <= 1) return $array;
$array_count = count($array);
$key = $array[0];
$left_arr = array();
$right_arr = array();
for ($i=1; $i<$array_count; $i++){
if ($array[$i] <= $key){
$left_arr[] = $array[$i];
}else{
$right_arr[] = $array[$i];
}
}
$left_arr = quick_sort($left_arr);
$right_arr = quick_sort($right_arr);
return array_merge($left_arr, array($key), $right_arr);
}
冒泡排序
$list = [2, 4, 1, 7, 9, 3];
$len = count($list);
for ($i = $len - 1; $i > 0; $i--) {
$flag = 1;
for ($j = 0; $j < $i; $j++) {
if ($list[$j] > $list[$j + 1]) {
$tmp = $list[$j];
$list[$j] = $list[$j + 1];
$list[$j + 1] = $tmp;
$flag = 0;
}
}
if($flag) break;
}
var_dump($list);
二分查找
function bin_sch($array, $low, $high, $k){
if ($low <= $high){
$mid = intval(($low+$high)/2);
if ($array[$mid] == $k){
return $mid;
}elseif ($k < $array[$mid]){
return bin_sch($array, $low, $mid-1, $k);
}else{
return bin_sch($array, $mid+1, $high, $k);
}
}
return -1;
}
顺序查找
function seq_sch($array, $n, $k){
$array[$n] = $k;
for($i=0; $i<$n; $i++){
if($array[$i]==$k){
break;
}
}
if ($i<$n){
return $i;
}else{
return -1;
}
}
插入排序
function insertSort($arr)
{
$count = count($arr);
for ($i = 1; $i < $count; $i++) {
$tmp = $arr[$i];
for ($j = $i - 1; $j >= 0; $j--) {
if ($tmp < $arr[$j]) {
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $tmp;
} else {
break;
}
}
}
return $arr;
}
选择排序
function selectSort($arr){
for ($i=1;$i<count($arr);$i++){
$p = $i;
for ($j = $i + 1; $j < count($arr);$j++){
if ($arr[$p] > $arr[$j]){
$p = $j;
}
}
if ($p != $i){
$tmp = $arr[$p];
$arr[$i] = $tmp;
$arr[$p] = $arr[$i];
}
}
return $arr;
}
字符串反转
<?php
function str_rev ($str) {
for ($i = 0; true; $i++)
{
if (!isset($str[$i])) break;
}
$return_str = '';
for ($j = $i - 1; $j >=0 ; $j -- )
{
$return_str .= $str[$j];
}
return $return_str;
}
function str_rev($str,$encoding='utf-8'){
$result = '';
$len = mb_strlen($str);
for($i=$len-1; $i>=0; $i--){
$result .= mb_substr($str,$i,1,$encoding);
}
return $result;
}
字符串长度
function strlen($str) {
if ($str == '') return 0;
$count = 0;
while (1){
if ($str[$count] != NULL){
$count++;
continue;
}else{
break;
}
}
return $count;
}
从url链接中取出文件扩展名
function getExt($url)
{
$arr = parse_url($url);
$file = basename($arr['path']);
$ext = explode('.', $file);
return $ext[count($ext)-1];
}
二维数组排序函数
function array_sort($arrays,$sort_key,$sort_order=SORT_DESC,$sort_type=SORT_NUMERIC ){
if(is_array($arrays)){
foreach ($arrays as $array){
if(is_array($array)){
$key_arrays[] = $array[$sort_key];
}else{
return false;
}
}
}else{
return false;
}
array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
return $arrays;
}
遍历文件夹
$dir = __DIR__;
if (is_dir($dir)) {
$array = scandir($dir, 1);
foreach($array as $key => $value) {
if ($value == '.' || $value == '..') {
unset($array[$key]);
continue;
}
}
} else {
echo '不是一个目录';
}
print_r($array);
遍历文件目录
function loopDir($dir){
$handle = opendir($dir);
while(false !==($file =readdir($handle))){
if($file!='.'&&$file!='..'){
echo $file."<br>";
if(filetype($dir.'/'.$file)=='dir'){
loopDir($dir.'/'.$file);
}
}
}
}
$dir = '/';
loopDir($dir);
遍历某个目录下面的所有文件和文件夹(包含子文件夹的目录和文件也要依次读取出来)
$dir = __DIR__;
function my_dir($dir) {
$files = array();
if(@$handle = opendir($dir)) {
while(($file = readdir($handle)) !== false) {
if($file != ".." && $file != ".") {
if(is_dir($dir."/".$file)) {
$files[$file] = my_dir($dir."/".$file);
} else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}
print_r(my_dir($dir));
写一个函数将open_door 转换为OpenDoor
function ucstring($string){
return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
}
function ucstring($string){
$array = explode('_', $string);
foreach($array as $key=>$val){
$new_string .= ucwords($val);
}
return $new_string;
}
写一个函数, 将 1234567890 转为 1,234,567,890 逗号隔开
function numFormate($number){
$str = (string) $number;
$string = strrev($str);
$length = strlen($string);
for($i = 0; $i < $length; $i = $i+3)
{
$new_string .= substr($string, $i, 3) . ',';
}
return strrev(rtrim($new_string, ','));
}
取扩展名
function get_ext1($file_name){
return strrchr($file_name, ‘.’);
}
function get_ext2($file_name){
return substr($file_name,strrpos($file_name, ‘.’));
}
function get_ext3($file_name){
return array_pop(explode(‘.’, $file_name));
}
function get_ext4($file_name){
$p = pathinfo($file_name);
return $p['extension'];
}
function get_ext5($file_name){
return strrev(substr(strrev($file_name), 0, strpos(strrev($file_name), ‘.’)));
}
function getExt($url){
$arr = parse_url($url);
$file = basename($arr['path']);
$ext = explode(“.”,$file);
return $ext[1];
}
求两个日期的差数, 例如2022-2-5 ~ 2022-3-6 的日期差数
function get_days($date1, $date2){
$time1 = strtotime($date1);
$time2 = strtotime($date2);
return ($time2-$time1)/86400;
}
PHP 打印出前一天的时间, 格式: 2022年01月01号 12:00:00
echo date("Y年m月d号 H:i:s", strtotime("-1 day"));
写出一个函数,参数为年份和月份,输出结果为指定月的天数
function getDayCount($year, $month) {
$date_string = $year . '-' . $month . '-1';
return date('t', strtotime($date_string));
}
获取今天是本月的第几周
echo ceil(date("t")/7);
单例实现mysql连接
class Db {
private static $instance;
public $handle;
Private function __construct($host,$username,$password,$dbname) {
$this->handle=NULL;
$this->getcon($host,$username,$password,$dbname);
}
public static function getBb() {
self::$instance=new Db();
return self::$instance;
}
private function getcon($host,$username,$password,$dbname) {
if($this->handle!=NULL){
return true;
}
$this->handle=mysqli_connect($host,$username,$password,$dbname);
}
}