Table of Contents
1. PHP encryption and decryption
2. PHP generates random strings
3. PHP gets the file extension (suffix)
4. PHP gets the file size and formats it
5. PHP replace tag characters
6. PHP lists the file names in the directory
7. PHP gets the URL of the current page
8. PHP forced download of files
9. PHP intercepts the string length
10. PHP gets the real IP of the client
11. PHP prevents SQL injection
12. PHP page prompts and jumps
13. PHP calculation time
Home Backend Development PHP Tutorial Sharing of commonly used PHP operation functions

Sharing of commonly used PHP operation functions

Mar 13, 2018 am 11:24 AM
php function share


1. PHP encryption and decryption

PHP encryption and decryption functions can be used to encrypt some useful strings and store them in the database, and through reversible decryption of the strings, the The function uses base64 and MD5 encryption and decryption.

1

2

3

4

5

6

7

8

9

function encryptDecrypt($key, $string, $decrypt){

    if($decrypt){

        $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");

        return $decrypted;

    }else{

        $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));

        return $encrypted;

    }

}

Copy after login

The usage method is as follows:

1

2

//以下是将字符串“Helloweba欢迎您”分别加密和解密 //加密: echo encryptDecrypt('password', 'Helloweba欢迎您',0);

//解密: echo encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);

Copy after login

2. PHP generates random strings

When we need to generate a random name, temporary password and other strings, we can use the following Function:

1

2

3

4

5

6

7

8

function generateRandomString($length = 10) {

    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    $randomString = '';

    for ($i = 0; $i < $length; $i++) {

        $randomString .= $characters[rand(0, strlen($characters) - 1)];

    }

    return $randomString;

}

Copy after login

The usage method is as follows:

1

echo generateRandomString(20);

Copy after login

3. PHP gets the file extension (suffix)

The following function can quickly get the file extension, that is, the suffix.

1

2

3

4

function getExtension($filename){

  $myext = substr($filename, strrpos($filename, &#39;.&#39;));

  return str_replace(&#39;.&#39;,&#39;&#39;,$myext);

}

Copy after login

The usage method is as follows:

1

2

$filename = &#39;我的文档.doc&#39;;

echo getExtension($filename);

Copy after login

4. PHP gets the file size and formats it

The function used below can get the file size and convert it into KB that is easy to read. , MB and other formats.

1

2

3

4

5

6

7

8

function formatSize($size) {

    $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");

    if ($size == 0) { 

        return(&#39;n/a&#39;); 

    } else {

      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); 

    }

}

Copy after login

The usage method is as follows:

1

2

$thefile = filesize(&#39;test_file.mp3&#39;);

echo formatSize($thefile);

Copy after login

5. PHP replace tag characters

Sometimes we need to replace strings and template tags with specified content, you can use the following Function:

1

2

3

4

function stringParser($string,$replacer){

    $result = str_replace(array_keys($replacer), array_values($replacer),$string);

    return $result;

}

Copy after login

The usage is as follows:

1

2

3

4

$string = &#39;The {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself&#39;;

$replace_array = array(&#39;{b}&#39; => &#39;<b>&#39;,&#39;{/b}&#39; => &#39;</b>&#39;,&#39;{br}&#39; => &#39;<br />&#39;);

 

echo stringParser($string,$replace_array);

Copy after login

6. PHP lists the file names in the directory

If you want to list all the files in the directory, use the following code That’s it:

1

2

3

4

5

6

7

8

9

10

function listDirFiles($DirPath){

    if($dir = opendir($DirPath)){

         while(($file = readdir($dir))!== false){

                if(!is_dir($DirPath.$file))

                {

                    echo "filename: $file<br />";

                }

         }

    }

}

Copy after login

The usage is as follows:

1

listDirFiles(&#39;home/some_folder/&#39;);

Copy after login

7. PHP gets the URL of the current page

The following function can get the URL of the current page, whether it is http or https:

1

2

3

4

5

6

7

8

9

10

11

function curPageURL() {

    $pageURL = &#39;http&#39;;

    if (!empty($_SERVER[&#39;HTTPS&#39;])) {$pageURL .= "s";}

    $pageURL .= "://";

    if ($_SERVER["SERVER_PORT"] != "80") {

        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

    } else {

        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

    }

    return $pageURL;

}

Copy after login

The usage method is as follows:

1

echo curPageURL();

Copy after login

8. PHP forced download of files

Sometimes we don’t want the browser to directly open files, such as PDF files, but to download files directly, Then the following function can force the file to be downloaded. The application/octet-stream header type is used in the function.

1

2

3

4

5

6

7

8

9

10

function download($filename){

    if ((isset($filename))&&(file_exists($filename))){

       header("Content-length: ".filesize($filename));

       header(&#39;Content-Type: application/octet-stream&#39;);

       header(&#39;Content-Disposition: attachment; filename="&#39; . $filename . &#39;"&#39;);

       readfile("$filename");

    } else {

       echo "Looks like file does not exist!";

    }

}

Copy after login

The usage method is as follows:

1

download(&#39;/down/test_45f73e852.zip&#39;);

Copy after login

9. PHP intercepts the string length

We often encounter situations where we need to intercept the length of a string (including Chinese characters), such as The title cannot exceed how many characters. The excess length is represented by.... The following function can meet your needs.

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

/*

 Utf-8、gb2312都支持的汉字截取函数

 cut_str(字符串, 截取长度, 开始长度, 编码);

 编码默认为 utf-8

 开始长度默认为 0 */ function cutStr($string, $sublen, $start = 0, $code = &#39;UTF-8&#39;){

    if($code == &#39;UTF-8&#39;){

        $pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";

        preg_match_all($pa, $string, $t_string);

 

        if(count($t_string[0]) - $start > $sublen) return join(&#39;&#39;, array_slice($t_string[0], $start, $sublen))."...";

        return join(&#39;&#39;, array_slice($t_string[0], $start, $sublen));

    }else{

        $start = $start*2;

        $sublen = $sublen*2;

        $strlen = strlen($string);

        $tmpstr = &#39;&#39;;

 

        for($i=0; $i<$strlen; $i++){

            if($i>=$start && $i<($start+$sublen)){

                if(ord(substr($string, $i, 1))>129){

                    $tmpstr.= substr($string, $i, 2);

                }else{

                    $tmpstr.= substr($string, $i, 1);

                }

            }

            if(ord(substr($string, $i, 1))>129) $i++;

        }

        if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";

        return $tmpstr;

    }

}

Copy after login

The usage method is as follows:

1

2

$str = "jQuery插件实现的加载图片和页面效果";

echo cutStr($str,16);

Copy after login

10. PHP gets the real IP of the client

We often use the database to record the user’s IP. The following code can get the real IP of the client. IP:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

//获取用户真实IP function getIp() {

    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))

        $ip = getenv("HTTP_CLIENT_IP");

    else

        if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))

            $ip = getenv("HTTP_X_FORWARDED_FOR");

        else

            if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))

                $ip = getenv("REMOTE_ADDR");

            else

                if (isset ($_SERVER[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], "unknown"))

                    $ip = $_SERVER[&#39;REMOTE_ADDR&#39;];

                else

                    $ip = "unknown";

    return ($ip);

}

Copy after login

The usage method is as follows:

1

echo getIp();

Copy after login

11. PHP prevents SQL injection

When we query the database, for security reasons, we need to filter some illegal characters to prevent SQL For malicious injection, please take a look at the function:

1

2

3

4

5

6

7

8

9

function injCheck($sql_str) { 

    $check = preg_match(&#39;/select|insert|update|delete|&#39;|/*|*|../|./|union|into|load_file|outfile/&#39;, $sql_str);

    if ($check) {

        echo &#39;非法字符!!&#39;;

        exit;

    } else {

        return $sql_str;

    }

}

Copy after login

The usage method is as follows:

1

echo injCheck(&#39;1 or 1=1&#39;);

Copy after login

12. PHP page prompts and jumps

When we perform form operations, sometimes in order to be friendly If you need to prompt the user for the operation result and jump to the relevant page, please see the following function:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

function message($msgTitle,$message,$jumpUrl){

    $str = &#39;<!DOCTYPE HTML>&#39;;

    $str .= &#39;<html>&#39;;

    $str .= &#39;<head>&#39;;

    $str .= &#39;<meta charset="utf-8">&#39;;

    $str .= &#39;<title>页面提示</title>&#39;;

    $str .= &#39;<style type="text/css">&#39;;

    $str .= &#39;*{margin:0; padding:0}a{color:#369; text-decoration:none;}a:hover{text-decoration:underline}body{height:100%; font:12px/18px Tahoma, Arial,  sans-serif; color:#424242; background:#fff}.message{width:450px; height:120px; margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; line-height:28px; background:#2c91c6; text-align:center; color:#fff; font-size:14px}.msg_txt{padding:10px; margin-top:8px}.msg_txt h4{line-height:26px; font-size:14px}.msg_txt h4.red{color:#f30}.msg_txt p{line-height:22px}&#39;;

    $str .= &#39;</style>&#39;;

    $str .= &#39;</head>&#39;;

    $str .= &#39;<body>&#39;;

    $str .= &#39;<p class="message">&#39;;

    $str .= &#39;<h3>&#39;.$msgTitle.&#39;</h3>&#39;;

    $str .= &#39;<p class="msg_txt">&#39;;

    $str .= &#39;<h4 class="red">&#39;.$message.&#39;</h4>&#39;;

    $str .= &#39;<p>系统将在 <span style="color:blue;font-weight:bold">3</span> 秒后自动跳转,如果不想等待,直接点击 <a href="{$jumpUrl}">这里</a> 跳转</p>&#39;;

    $str .= "<script>setTimeout(&#39;location.replace(&#39;".$jumpUrl."&#39;)&#39;,2000)</script>";

    $str .= &#39;</p>&#39;;

    $str .= &#39;</p>&#39;;

    $str .= &#39;</body>&#39;;

    $str .= &#39;</html>&#39;;

    echo $str;

}

Copy after login

The usage is as follows:

1

message(&#39;操作提示&#39;,&#39;操作成功!&#39;,&#39;https://segmentfault.com/&#39;);

Copy after login

13. PHP calculation time

We are processing it time, it is necessary to calculate the length of time from the current time to a certain point in time. For example, to calculate the running time of the client, hh:mm:ss is usually used to represent

1

2

3

4

5

6

7

8

9

10

function changeTimeType($seconds) {

    if ($seconds > 3600) {

        $hours = intval($seconds / 3600);

        $minutes = $seconds % 3600;

        $time = $hours . ":" . gmstrftime(&#39;%M:%S&#39;, $minutes);

    } else {

        $time = gmstrftime(&#39;%H:%M:%S&#39;, $seconds);

    }

    return $time;

}

Copy after login

The usage method is as follows:

1

2

$seconds = 3712;

echo changeTimeType($seconds);

Copy after login

The following is Obtain client IP, string interception, download, etc. For details, please view the following code:

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

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

<?php/**

 * 获取客户端IP

 * @return [string] [description]

 */function getClientIp() {

 $ip = NULL; if (isset($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;])) {  $arr = explode(&#39;,&#39;, $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]);  $pos = array_search(&#39;unknown&#39;,$arr);  if(false !== $pos) unset($arr[$pos]);  $ip = trim($arr[0]);

 }elseif (isset($_SERVER[&#39;HTTP_CLIENT_IP&#39;])) {  $ip = $_SERVER[&#39;HTTP_CLIENT_IP&#39;];

 }elseif (isset($_SERVER[&#39;REMOTE_ADDR&#39;])) {  $ip = $_SERVER[&#39;REMOTE_ADDR&#39;];

 } // IP地址合法验证

 $ip = (false !== ip2long($ip)) ? $ip : &#39;0.0.0.0&#39;; return $ip;

}/**

 * 获取在线IP

 * @return String

 */function getOnlineIp($format=0) {

 global $S_GLOBAL; if(empty($S_GLOBAL[&#39;onlineip&#39;])) {  if(getenv(&#39;HTTP_CLIENT_IP&#39;) && strcasecmp(getenv(&#39;HTTP_CLIENT_IP&#39;), &#39;unknown&#39;)) {   $onlineip = getenv(&#39;HTTP_CLIENT_IP&#39;);

  } elseif(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;) && strcasecmp(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;), &#39;unknown&#39;)) {   $onlineip = getenv(&#39;HTTP_X_FORWARDED_FOR&#39;);

  } elseif(getenv(&#39;REMOTE_ADDR&#39;) && strcasecmp(getenv(&#39;REMOTE_ADDR&#39;), &#39;unknown&#39;)) {   $onlineip = getenv(&#39;REMOTE_ADDR&#39;);

  } elseif(isset($_SERVER[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], &#39;unknown&#39;)) {   $onlineip = $_SERVER[&#39;REMOTE_ADDR&#39;];

  }

  preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);  $S_GLOBAL[&#39;onlineip&#39;] = $onlineipmatches[0] ? $onlineipmatches[0] : &#39;unknown&#39;;

 } if($format) {  $ips = explode(&#39;.&#39;, $S_GLOBAL[&#39;onlineip&#39;]);  for($i=0;$i<3;$i++) {   $ips[$i] = intval($ips[$i]);

  return sprintf(&#39;%03d%03d%03d&#39;, $ips[0], $ips[1], $ips[2]);

 } else return $S_GLOBAL[&#39;onlineip&#39;];

 }

}/**

 * 获取url

 * @return [type] [description]

 */function getUrl(){

 $pageURL = &#39;http&#39;; if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") { $pageURL .= "s";

 } $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["HTTP_HOST"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];

 } else { $pageURL .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];

 } return $pageURL;

}/**

 * 获取当前站点的访问路径根目录

 * @return [type] [description]

 */function getSiteUrl() {

 $uri = $_SERVER[&#39;REQUEST_URI&#39;]?$_SERVER[&#39;REQUEST_URI&#39;]:($_SERVER[&#39;PHP_SELF&#39;]?$_SERVER[&#39;PHP_SELF&#39;]:$_SERVER[&#39;SCRIPT_NAME&#39;]); return &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].substr($uri, 0, strrpos($uri, &#39;/&#39;)+1);

}/**

 * 字符串截取,支持中文和其他编码

 * @param [string] $str  [字符串]

 * @param integer $start [起始位置]

 * @param integer $length [截取长度]

 * @param string $charset [字符串编码]

 * @param boolean $suffix [是否有省略号]

 * @return [type]   [description]

 */function msubstr($str, $start=0, $length=15, $charset="utf-8", $suffix=true) {

 if(function_exists("mb_substr")) {  return mb_substr($str, $start, $length, $charset);

 } elseif(function_exists(&#39;iconv_substr&#39;)) {  return iconv_substr($str,$start,$length,$charset);

 } $re[&#39;utf-8&#39;] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re[&#39;gb2312&#39;] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re[&#39;gbk&#39;] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re[&#39;big5&#39;] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";

 preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); if($suffix) {  return $slice."…";

 } return $slice;

}/**

 * php 实现js escape 函数

 * @param [type] $string [description]

 * @param string $encoding [description]

 * @return [type]   [description]

 */function escape($string, $encoding = &#39;UTF-8&#39;){

 $return = null; for ($x = 0; $x < mb_strlen($string, $encoding);$x ++)

 { $str = mb_substr($string, $x, 1, $encoding); if (strlen($str) > 1) { // 多字节字符

  $return .= "%u" . strtoupper(bin2hex(mb_convert_encoding($str, &#39;UCS-2&#39;, $encoding)));

 } else $return .= "%" . strtoupper(bin2hex($str));

 }

 } return $return;

}/**

 * php 实现 js unescape函数

 * @param [type] $str [description]

 * @return [type]  [description]

 */function unescape($str) {

 $str = rawurldecode($str);

 preg_match_all("/(?:%u.{4})|.{4};|&#\d+;|.+/U",$str,$r); $ar = $r[0]; foreach($ar as $k=>$v) {  if(substr($v,0,2) == "%u"){   $ar[$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));

  } elseif(substr($v,0,3) == "") {   $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));

  } elseif(substr($v,0,2) == "&#") {   echo substr($v,2,-1)."";   $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));

  }

 } return join("",$ar);

}/**

 * 数字转人名币

 * @param [type] $num [description]

 * @return [type]  [description]

 */function num2rmb ($num) {

 $c1 = "零壹贰叁肆伍陆柒捌玖"; $c2 = "分角元拾佰仟万拾佰仟亿"; $num = round($num, 2); $num = $num * 100; if (strlen($num) > 10) {  return "oh,sorry,the number is too long!";

 } $i = 0; $c = ""; while (1) {  if ($i == 0) {   $n = substr($num, strlen($num)-1, 1);

  } else {   $n = $num % 10;

  $p1 = substr($c1, 3 * $n, 3);  $p2 = substr($c2, 3 * $i, 3);  if ($n != &#39;0&#39; || ($n == &#39;0&#39; && ($p2 == &#39;亿&#39; || $p2 == &#39;万&#39; || $p2 == &#39;元&#39;))) {   $c = $p1 . $p2 . $c;

  } else {   $c = $p1 . $c;

  $i = $i + 1;  $num = $num / 10;  $num = (int)$numif ($num == 0) {   break;

  }

 } $j = 0; $slen = strlen($c); while ($j < $slen) {  $m = substr($c, $j, 6);  if ($m == &#39;零元&#39; || $m == &#39;零万&#39; || $m == &#39;零亿&#39; || $m == &#39;零零&#39;) {   $left = substr($c, 0, $j);   $right = substr($c, $j + 3);   $c = $left . $right;   $j = $j-3;   $slen = $slen-3;

  $j = $j + 3;

 } if (substr($c, strlen($c)-3, 3) == &#39;零&#39;) {  $c = substr($c, 0, strlen($c)-3);

 } // if there is a &#39;0&#39; on the end , chop it out

 return $c . "整";

}/**

 * 特殊的字符

 * @param [type] $str [description]

 * @return [type]  [description]

 */function makeSemiangle($str) {

 $arr = array(  &#39;0&#39; => &#39;0&#39;, &#39;1&#39; => &#39;1&#39;, &#39;2&#39; => &#39;2&#39;, &#39;3&#39; => &#39;3&#39;, &#39;4&#39; => &#39;4&#39;,  &#39;5&#39; => &#39;5&#39;, &#39;6&#39; => &#39;6&#39;, &#39;7&#39; => &#39;7&#39;, &#39;8&#39; => &#39;8&#39;, &#39;9&#39; => &#39;9&#39;,  &#39;A&#39; => &#39;A&#39;, &#39;B&#39; => &#39;B&#39;, &#39;C&#39; => &#39;C&#39;, &#39;D&#39; => &#39;D&#39;, &#39;E&#39; => &#39;E&#39;,  &#39;F&#39; => &#39;F&#39;, &#39;G&#39; => &#39;G&#39;, &#39;H&#39; => &#39;H&#39;, &#39;I&#39; => &#39;I&#39;, &#39;J&#39; => &#39;J&#39;,  &#39;K&#39; => &#39;K&#39;, &#39;L&#39; => &#39;L&#39;, &#39;M&#39; => &#39;M&#39;, &#39;N&#39; => &#39;N&#39;, &#39;O&#39; => &#39;O&#39;,  &#39;P&#39; => &#39;P&#39;, &#39;Q&#39; => &#39;Q&#39;, &#39;R&#39; => &#39;R&#39;, &#39;S&#39; => &#39;S&#39;, &#39;T&#39; => &#39;T&#39;,  &#39;U&#39; => &#39;U&#39;, &#39;V&#39; => &#39;V&#39;, &#39;W&#39; => &#39;W&#39;, &#39;X&#39; => &#39;X&#39;, &#39;Y&#39; => &#39;Y&#39;,  &#39;Z&#39; => &#39;Z&#39;, &#39;a&#39; => &#39;a&#39;, &#39;b&#39; => &#39;b&#39;, &#39;c&#39; => &#39;c&#39;, &#39;d&#39; => &#39;d&#39;,  &#39;e&#39; => &#39;e&#39;, &#39;f&#39; => &#39;f&#39;, &#39;g&#39; => &#39;g&#39;, &#39;h&#39; => &#39;h&#39;, &#39;i&#39; => &#39;i&#39;,  &#39;j&#39; => &#39;j&#39;, &#39;k&#39; => &#39;k&#39;, &#39;l&#39; => &#39;l&#39;, &#39;m&#39; => &#39;m&#39;, &#39;n&#39; => &#39;n&#39;,  &#39;o&#39; => &#39;o&#39;, &#39;p&#39; => &#39;p&#39;, &#39;q&#39; => &#39;q&#39;, &#39;r&#39; => &#39;r&#39;, &#39;s&#39; => &#39;s&#39;,  &#39;t&#39; => &#39;t&#39;, &#39;u&#39; => &#39;u&#39;, &#39;v&#39; => &#39;v&#39;, &#39;w&#39; => &#39;w&#39;, &#39;x&#39; => &#39;x&#39;,  &#39;y&#39; => &#39;y&#39;, &#39;z&#39; => &#39;z&#39;,  &#39;(&#39; => &#39;(&#39;, &#39;)&#39; => &#39;)&#39;, &#39;〔&#39; => &#39;[&#39;, &#39;〕&#39; => &#39;]&#39;, &#39;【&#39; => &#39;[&#39;,  &#39;】&#39; => &#39;]&#39;, &#39;〖&#39; => &#39;[&#39;, &#39;〗&#39; => &#39;]&#39;, &#39;{&#39; => &#39;{&#39;, &#39;}&#39; => &#39;}&#39;, &#39;《&#39; => &#39;<&#39;,  &#39;》&#39; => &#39;>&#39;,  &#39;%&#39; => &#39;%&#39;, &#39;+&#39; => &#39;+&#39;, &#39;—&#39; => &#39;-&#39;, &#39;-&#39; => &#39;-&#39;, &#39;~&#39; => &#39;-&#39;,  &#39;:&#39; => &#39;:&#39;, &#39;。&#39; => &#39;.&#39;, &#39;、&#39; => &#39;,&#39;, &#39;,&#39; => &#39;.&#39;, &#39;、&#39; => &#39;.&#39;,  &#39;;&#39; => &#39;;&#39;, &#39;?&#39; => &#39;?&#39;, &#39;!&#39; => &#39;!&#39;, &#39;…&#39; => &#39;-&#39;, &#39;‖&#39; => &#39;|&#39;,  &#39;”&#39; => &#39;"&#39;, &#39;“&#39; => &#39;"&#39;, &#39;&#39;&#39; => &#39;`&#39;, &#39;‘&#39; => &#39;`&#39;, &#39;|&#39; => &#39;|&#39;, &#39;〃&#39; => &#39;"&#39;,

  &#39; &#39; => &#39; &#39;,&#39;.&#39; => &#39;.&#39;);

 return strtr($str, $arr);

}

 

/**

 * 下载

 * @param [type] $filename [description]

 * @param string $dir  [description]

 * @return [type]   [description]

 */

function downloads($filename,$dir=&#39;./&#39;){

 $filepath = $dir.$filename;

 if (!file_exists($filepath)){

  header("Content-type: text/html; charset=utf-8");

  echo "File not found!";

  exit;

 } else {

  $file = fopen($filepath,"r");

  Header("Content-type: application/octet-stream");

  Header("Accept-Ranges: bytes");

  Header("Accept-Length: ".filesize($filepath));

  Header("Content-Disposition: attachment; filename=".$filename);

  echo fread($file, filesize($filepath));

  fclose($file);

 }

}

 

/**

 * 创建一个目录树

 * @param [type] $dir [description]

 * @param integer $mode [description]

 * @return [type]  [description]

 */

function mkdirs($dir, $mode = 0777) {

 if (!is_dir($dir)) {

  mkdirs(dirname($dir), $mode);

  return mkdir($dir, $mode);

 }

 return true;

}

Copy after login

Related recommendations:

Summary of string operation functions in php

php delete folder operation function and several methods example code summary

php summary of commonly used string operation functions

The above is the detailed content of Sharing of commonly used PHP operation functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles