Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common php self-built functions in some php projects?

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces some php projects in the more general php self-built functions which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

Some of the following php functions are the most commonly used project development functions of our it Power, which are used in more projects and are more general.

1. The handler of the request interface

The copy code is as follows:

/ * *

* curl access program interface

* @ param string

* @ return array

, /

Function getCurlDate ($url, $datas, $key) {

$datas ['time'] = $_ SERVER [' REQUEST_TIME'] + 300

$post_data ['post'] = urlencode (authcode (serialize ($datas), "ENCODE", $key))

/ / echo $url

$ch = curl_init ()

Curl_setopt ($ch, CURLOPT_URL, $url)

Curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1)

/ / We are in POST data!

Curl_setopt ($ch, CURLOPT_POST, 1)

/ / add the variable of post

Curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_data)

$output = curl_exec ($ch)

/ / dump (curl_error ($ch))

Curl_close ($ch)

Return json_decode ($output, true)

}

two。 Get file extension

The copy code is as follows:

/ * *

* @ get the file extension

* @ $pic string picture path

, /

Function get_file_ext ($pic) {

Return substr ($pic, strrpos ($pic,'.) + 1)

}

3. Reversible encryption and decryption function

The copy code is as follows:

/ * *

* string encryption

* @ param $string characters to be encrypted

* @ param $operation encryption or decryption

* @ param $key website encrypts key to prevent cracking

* @ return string

, /

Function authcode ($string, $operation = 'DECODE', $key ='', $expiry = 0) {

$ckey_length = 4

$key = md5 ($key? $key:'^ www.itokit.com$')

$keya = md5 (substr ($key, 0,16))

$keyb = md5 (substr ($key, 16,16))

$keyc = $ckey_length? ($operation = = 'DECODE'? Substr ($string, 0, $ckey_length): substr (md5 (microtime ()),-$ckey_length):''

$cryptkey = $keya. Md5 ($keya. $keyc)

$key_length = strlen ($cryptkey)

$string = $operation = = 'DECODE'? Base64_decode (substr ($string, $ckey_length): sprintf ('0dink, $expiry? $expiry + time (): 0). Substr (md5 ($string. $keyb), 0,16). $string

$string_length = strlen ($string)

$result =''

$box = range (0,255)

$rndkey = array ()

For ($I = 0; $I 0) & & substr ($result, 10,16) = = substr (md5 (substr ($result, 26). $keyb), 0,16)) {

Return substr ($result, 26)

} else {

Return''

}

} else {

Return $keyc. Str_replace ('=',', base64_encode ($result))

}

}

4. String to hexadecimal

The copy code is as follows:

/ * *

* convert string to hexadecimal

* @ param unknown_type $s

, /

Function str2hex ($s) {

$r = ""

$hexes = array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f")

For ($iTun0; $I > 4)]. $hexes [(ord ($s {$I}) & 0xf)])

Return $r

}

5. Hexadecimal to string

The copy code is as follows:

/ * *

* hexadecimal to string

* @ param unknown_type $s

, /

Function hex2str ($s) {

$r = ""

For ($I = 0; $iTunes 48 & & $x1mm 48 & & $x2 $val) $string [$key] = new_stripslashes ($val)

Return $string

}

/ * /

Function stripslashes_deep ($string)

{

Return is_array ($string)? Array_map ('stripslashes_deep', $string): stripslashes ($string)

}

8. Returns a string or array processed by htmlspecialchars

The copy code is as follows:

/ * *

* returns a string or array processed by htmlspecialchars

* @ param $string strings or arrays to be processed

* @ return mixed

, /

Function new_html_special_chars ($string) {

If (! is_array ($string)) return htmlspecialchars ($string)

Foreach ($string as $key = > $val) $string [$key] = new_html_special_chars ($val)

Return $string

}

9. Get request ip

The copy code is as follows:

/ * *

* get the request ip

*

* @ return ip address

, /

Function ip () {

If (getenv ('HTTP_CLIENT_IP') & & strcasecmp (getenv (' HTTP_CLIENT_IP'), 'unknown')) {

$ip = getenv ('HTTP_CLIENT_IP')

} elseif (getenv ('HTTP_X_FORWARDED_FOR') & & strcasecmp (getenv (' HTTP_X_FORWARDED_FOR'), 'unknown')) {

$ip = getenv ('HTTP_X_FORWARDED_FOR')

} elseif (getenv ('REMOTE_ADDR') & & strcasecmp (getenv (' REMOTE_ADDR'), 'unknown')) {

$ip = getenv ('REMOTE_ADDR')

} elseif (isset ($_ SERVER ['REMOTE_ADDR']) & & $_ SERVER [' REMOTE_ADDR'] & & strcasecmp ($_ SERVER ['REMOTE_ADDR'],' unknown')) {

$ip = $_ SERVER ['REMOTE_ADDR']

}

Return preg_match ('/ [\ d\.] {7 ip 15} /', $ip, $matches)? $matches [0]:''

}

10. Character interception supports UTF8/GBK

The copy code is as follows:

/ * *

* character intercepting supports UTF8/GBK

* @ param $string

* @ param $length

* @ param $dot

, /

Function str_cut ($string, $length, $dot = '...') {

$strlen = strlen ($string)

If ($strlen 127? $string [$I]. $string [+ + $I]: $string [$I]

If (in_array ($current_str, $search_arr)) {

$key = $search_flip [$current_str]

$current_str = str_replace ($search_arr [$key], $replace_arr [$key], $current_str)

}

$strcut. = $current_str

}

}

Return $strcut.$dot

}

11. Generate random strings

The copy code is as follows:

/ * *

* generate random strings

*

* @ param int $length output length

* @ param string $chars is optional, default is 0123456789

* @ return string string

, /

Function random ($length, $chars = '0123456789') {

$hash =''

$max = strlen ($chars)-1

For ($I = 0; $I

< $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } 12.将字符串转换为数组 复制代码 代码如下: /** * 将字符串转换为数组 * * @param string $data 字符串 * @return array 返回数组格式,如果,data为空,则返回空数组 */ function string2array($data) { if($data == '') return array(); eval("\$array = $data;"); return $array; } 13.将数组转换为字符串 复制代码 代码如下: /** * 将数组转换为字符串 * * @param array $data 数组 * @param bool $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1 * @return string 返回字符串,如果,data为空,则返回空 */ function array2string($data, $isformdata = 1) { if($data == '') return ''; if($isformdata) $data = new_stripslashes($data); return addslashes(var_export($data, TRUE)); } 14.转换字节数为其他单位 复制代码 代码如下: /** * 转换字节数为其他单位 * * * @param string $filesize 字节大小 * @return string 返回大小 */ function sizecount($filesize) { if ($filesize >

= 1073741824) {

$filesize = round ($filesize / 1073741824 * 100) / 1000.' GB'

} elseif ($filesize > = 1048576) {

$filesize = round ($filesize / 1048576 * 100) / 1000.' MB'

} elseif ($filesize > = 1024) {

$filesize = round ($filesize / 1024 * 100) / 100. ' KB'

} else {

$filesize = $filesize.' Bytes'

}

Return $filesize

}

Thank you for reading this article carefully. I hope the article "what are the common php self-built functions in some php projects" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report