-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.php
More file actions
62 lines (60 loc) · 1.52 KB
/
url.php
File metadata and controls
62 lines (60 loc) · 1.52 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace ScpoPHP;
/**
* url操作相关
* @link http://scpo-php.seventop.top/rewrite/
*/
class Url
{
/**
* 获取域名
* @param bool $port 是否包含端口
* @return string 域名
*/
static public function dmn($port = false)
{
static $dmn = array();
if (isset($dmn[1])) {
if ($port) return $dmn[1];
if (isset($dmn[0])) return $dmn[0];
} else {
$dmn[1] = explode('.', $_SERVER['HTTP_HOST']);
$dmn[1] = array(array_pop($dmn[1]), array_pop($dmn[1]));
$dmn[1] = "{$dmn[1][1]}.{$dmn[1][0]}";
}
return $port
? $dmn[1]
: ($dmn[0] = ($pos = strpos($dmn[1], ':')) !== false
? substr($dmn[1], 0, $pos)
: $dmn[1]
);
}
/**
* 获取伪静态下访问的uri和get请求字符串
* @return array 数组,分别为uri和get请求
*/
static public function rewrite_uriget()
{
static $mem;
if ($mem) return $mem;
if (($pos = strpos($uri = $_SERVER['QUERY_STRING'], '&')) !== false) {
$get = substr($uri, $pos + 1);
$uri = substr($uri, 0, $pos);
} else $get = '';
return $mem = array($uri, $get);
}
/**
* 替换查询字符串
*/
static public function rep_query($url, $arr)
{
$base = ($qm = strpos($url, '?')) === false
? $url
: (($hash = strpos($url, '#', $qm)) === false
? substr($url, 0, $qm)
: substr($url, 0, $qm) . substr($url, $hash));
[$path, $hash] = array_pad(explode('#', $base, 2), 2, '');
$query = $arr ? '?' . http_build_query($arr, '', '&', PHP_QUERY_RFC3986) : '';
return $path . $query . ($hash === '' ? '' : '#' . $hash);
}
}