>> 本文固定链接: http://php.ncong.com/mianshi/mianshiti_string.html
>> 转载请注明: 恩聪php 2014年09月02日 于 恩聪PHP学习教程 发表
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php $str = "hello"; function fan($str) { //声明一个临时的变量 $n = ""; //获取字符串长度 $m = strlen($str)-1; for($i=$m; $i >= 0; $i--) { $n .= $str{$i}; } return $n; } echo fan($str);?> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php $str = "12345678932132"; function nformat($str) { $n = ""; //临时的变量 $m = strlen($str); //获取字符串长度 $k = $m % 3; //让整个长度和3取余之后余数是多少 = 0 for($i=0; $i < $m; $i++) { if($i%3 == $k && $i!=0) { $n .=","; } $n .= $str{$i}; } return $n; } echo nformat($str);?> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php function extname($url) { if(strstr($url, "?")) { //如果有问号格式的文件, 将问号前的文件取出给变量$file list($file) = explode("?", $url); } else { $file = $url; } //以下是第二步取出文件名 $loc = strrpos($file, "/")+1; $filename = substr($file, $loc); //以下是第三步取扩展名称 $arr = explode(".", $filename); //弹出数组最后一个元素 return array_pop($arr); } echo extname("http://www.lampbrother.net/aaa/init.inc.php")."<br>"; echo extname("init.inc.php")."<br>"; echo extname("C:/aaa/init.inc.php")."<br>"; echo extname("http://www.lampbrother.net/aaa/init.inc.php?a=100")."<br>";?> |
如:
$a=’/a/b/c/d/e.php’
$b=’/a/b/12/34/c.php’
计算出$b相对于$a的相对路径应该是../../c/d
|
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
|
<?php function abspath($a, $b) { //第一步去除公共的目录结构 // $a = "/a/b/c/d/e.php"; // $b = "/a/b/12/34/c.php"; $a = dirname($a); // /a/b/c/d $b = dirname($b); // /a/b/12/34 $a = trim($a, "/"); // a/b/c/d $b = trim($b, "/"); // a/b/12/34 $a = explode("/", $a); // array("a", "b", "c", "d") $b = explode("/", $b); // array("a", "b", "12", "34") //合并上面代码相当于 $a = explode("/", trim(dirname($a), "/")); $num = max(count($a), count($b)); for($i=0; $i<$num; $i++) { if($a[$i]==$b[$i]) { unset($a[$i]); unset($b[$i]); }else{ break; } } //$a = array("c", "d"); //$b = array("12", "34"); //第二步:回到同级目录, 进入另一个目录 $path = str_repeat("../", count($b)).implode("/", $a); // ../../c/d return $path; } $a = "/a/b/c/d/e/w/f/e.php"; $b = "/a/b/12/34/100/c.php"; // ../../c/d echo abspath($a, $b);?> |
原文:http://www.cnblogs.com/ncong/p/3952465.html