php-method

1,获取链接后缀

parse_url basename explode end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
//获取后缀
$url ="https://www.yisu.com/reg.php?id=22";
//先切分
$res = parse_url($url);
var_dump($res);
//获取basename
$path = basename($res["path"]);
var_dump($path);
//获取数组
$arr = explode(".", $path);
//获取最后一个元素
echo (end($arr));
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
输出结果:
array(4) {
["scheme"]=>
string(5) "https"
["host"]=>
string(12) "www.yisu.com"
["path"]=>
string(8) "/reg.php"
["query"]=>
string(5) "id=22"
}
array(2) {
[0]=>
string(3) "reg"
[1]=>
string(3) "php"
}
string(3) "php"
2,PHP代码确保多个进程同时写入同一个文件成功
fopen flock LOCK_EX LOCK_UN fwrite
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
echo "<pre>";
$handle = fopen("est.txt", "a+");
// 排它性的锁定
if (flock($handle,LOCK_EX)){
fwrite($handle,"W222");
// release lock
flock($handle,LOCK_UN);
}
else{
echo "Error locking file!";
}

fclose($handle);

$Reader = fopen("est.txt", "r");

$content = fread($Reader, filesize("est.txt"));

var_dump($content);
fclose($Reader)

?>
3,创建多级目录

mkdir is_dir

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
//创建多级目录
echo "<pre>";
$path = "php/php1/php2";
echo $path;
function create_dir($path,$mode=0777){
if(is_dir($path)){
return "目录已存在";
}else{
$res = mkdir($path,$mode,true);
if($res) {
return "创建成功";
}else{
return "创建失败";
}
}
}
$res = create_dir($path);
var_dump($res);

4,遍历文件夹

opendir readdir is_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
echo "<pre>";
function fileShow($dir){
$handle = opendir($dir);
$filelist = array();
while($file = readdir($handle)){
if ($file !=="." && $file !=="..") {
$lfile = $dir."/".$file;
if (is_file($lfile)){
//表示文件
$filelist[$dir][]=$lfile;
}else{
//文件夹
$filelist[$dir][]=fileShow($lfile);
}
}
$i++;
}
closedir($handle);
return $filelist;
}

$list = fileShow('.');

var_dump($list);
5,冒泡算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

$listnums = array(1,23,3,5,32,50);
//array(6) { [0]=> int(1) [1]=> int(23) [2]=> int(3) [3]=> int(5) [4]=> int(32) [5]=> int(50) }

//从后向前获取值
function showArray($listnums){
$lenth = count($listnums);
//确定要循环的次数
for ($i=1; $i < $lenth ; $i++) { //$i=1。因为是两两相比所以如果有6个数据需要对比5次 所以 < $lenth

for ($j=0; $j < $lenth - $i ; $j++) {
//索引取值每一层 最后一个值不要要对比 倒数第二个和它对比 $lenth - $i 也就是5次 索引0-4
//第一次循环 从第一个和第二个对比比 5次对比 获取一个最大值或者最小值 索引 0-4
//第二次循环已经获取到一个我们想要的值, 4次对比 最后一个值不需要对比... 索引 0-3 , 3次 索引 0-2 ,2次 索引 0-1 , 1次 出来结果

if ($listnums[$j] < $listnums[$j+1]) {
# 将最小的值对比到数组最后面
$t = $listnums[$j+1];
$listnums[$j+1] = $listnums[$j];
$listnums[$j] = $t;
}
}
}
return $listnums;
}
//从前向后获取
function showArray2($listnums){
$lenth = count($listnums);
//确定要循环的次数
for ($i=0; $i < $lenth; $i++) {
for ($j=$i+1; $j < $lenth; $j++) {
if($listnums[$i] < $listnums[$j]){
$t = $listnums[$j];
$listnums[$j] = $listnums[$i];
$listnums[$i] = $t;
}
}
}
return $listnums;
}
echo "<pre>";
$newnums = (showArray($listnums));
var_dump($newnums);

?>
6, 1234567890abcdefg 倒着输出

strrev or while . 连接符号的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
//反转函数1
$str = "1234567890abcdefg";
$newstr = strrev($str);
echo $newstr;

//=========2
$str = '1234567890abcdefg';
$newstr = '';
$i = 0;
while(isset($str[$i]) && $str[$i]!=null){
$newstr = $str[$i++].$newstr;
}
echo $newstr;