最全的PHP常用函数大全
- quotemeta() 函数在字符串中某些预定义的字符前添加反斜杠。
- quoted_printable_decode() 函数对经过 quoted-printable 编码后的字符串进行解码,返回 位的字符串。
- printf() 函数输出格式化的字符串。
- print() 函数输出一个或多个字符串。
- parse_str() 函数把查询字符串解析到变量中。
- ord() 函数返回字符串第一个字符的 ASCII 值。
- number_format() 函数通过千位分组来格式化数字。
- nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (<br />)。
- nl_langinfo() 函数返回指定的本地信息。
- money_format() 函数把字符串格式化为货币字符串。
- metaphone() 函数计算字符串的 metaphone 键。
- md5_file() 函数计算文件的 MD5 散列。
- md5() 函数计算字符串的 MD5 散列。
- ltrim() 函数从字符串左侧删除空格或其他预定义字符。
- localeconv() 函数返回包含本地数字及货币信息格式的数组。
- levenshtein() 函数返回两个字符串之间的 Levenshtein 距离。
- join() 函数把数组元素组合为一个字符串。
- implode() 函数把数组元素组合为一个字符串。
- htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。
- html_entity_decode() chars_decode() 函数
- htmlentities() 函数把字符转换为 HTML 实体。
- html_entity_decode() 函数把 HTML 实体转换为字符。
- hebrevc() 函数把希伯来文本从右至左的流转换为左至右的流。它也会把新行 (\n) 转换为 <br />。
- hebrev() 函数把希伯来文本从右至左的流转换为左至右的流。
- get_html_translation_table() 函数返回被 htmlentities() 和 htmlspecialchars() 函数使用的翻译表。
- fprintf() 函数把格式化的字符串写到指定的输出流(例如:文件或数据库)。
- explode() 函数把字符串分割为数组。
- echo() 函数输出一个或多个字符串。
- crypt() 函数返回使用 DES、Blowfish 或 MD5 加密的字符串。
- crc32() 函数计算一个字符串的 crc32 多项式。
- count_chars() 函数返回字符串所用字符的信息。
- convert_uuencode() 函数使用 uuencode 算法对字符串进行编码。
- convert_uudecode() 函数对 uuencode 编码的字符串进行解码。
- convert_cyr_string() 函数把字符由一种 Cyrillic 字符转换成另一种。
- chunk_split() 函数把字符串分割为一连串更小的部分。
- chr() 函数从指定的 ASCII 值返回字符。
- chop() 函数从字符串的末端开始删除空白字符或其他预定义字符。
- bin2hex() 函数把 ASCII 字符的字符串转换为十六进制值。
- addslashes() 函数在指定的预定义字符前添加反斜杠。
- addcslashes() 函数在指定的字符前添加反斜杠。
- xpath() 函数运行对 XML 文档的 XPath 查询。
- simplexml_load_string() 函数把 XML 字符串载入对象中。
- simplexml_load_file() 函数把 XML 文档载入对象中。
- simplexml_import_dom() 函数把 DOM 节点转换为 SimpleXMLElement 对象。
- registerXPathNamespace() 函数为下一次 XPath 查询创建命名空间语境。
- getNamespace() 函数获取在 XML 文档中使用的命名空间。
- getName() 函数从 SimpleXMLElement 对象获取 XML 元素的名称。
- getDocNamespaces() 函数从 SimpleXMLElement 对象返回在 XML 文档中声明的命名空间。
- children() 函数获取指定节点的子节点。
- attributes() 函数获取 SimpleXML 元素的属性。
- asXML() 函数以字符串的形式从 SimpleXMLElement 对象返回 XML 文档。
- addChild() 函数向指定的 XML 节点添加一个子节点。
- addAttribute() 函数给 SimpleXML 元素添加一个属性。
- __construct() 函数创建一个新的 SimpleXMLElement 对象。
- mysql_unbuffered_query() 函数向 MySQL 发送一条 SQL 查询(不获取 / 缓存结果)。
- mysql_thread_id() 函数返回当前线程的 ID。
- mysql_stat() 函数返回 MySQL 服务器的当前系统状态。
- mysql_select_db() 函数设置活动的 MySQL 数据库。
- mysql_result() 函数返回结果集中一个字段的值。
- mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符。
- mysql_query() 函数执行一条 MySQL 查询。
- mysql_ping() 函数 Ping 一个服务器连接,如果没有连接则重新连接。
- mysql_pconnect() 函数打开一个到 MySQL 服务器的持久连接。
- mysql_num_rows() 函数返回结果集中行的数目。
- mysql_num_fields() 函数返回结果集中字段的数。
- mysql_list_processes() 函数列出 MySQL 进程。
- mysql_list_dbs() 函数列出 MySQL 服务器中所有的数据库。
- mysql_insert_id() 函数返回上一步 INSERT 操作产生的 ID。
- mysql_info() 函数返回最近一条查询的信息。
- mysql_get_server_info() 函数返回 MySQL 服务器的信息。
- mysql_get_proto_info() 函数返回 MySQL 协议的信息。
- mysql_get_host_info() 函数返回 MySQL 主机的信息。
- mysql_get_client_info() 函数返回 MySQL 客户端信息。
- mysql_free_result() 函数释放结果内存。
- mysql_field_type() 函数返回结果集中指定字段的类型。
- mysql_field_table() 函数返回指定字段所在的表名。
- mysql_field_seek() 函数将结果集中的指针设定为指定的字段偏移量。
- mysql_field_name() 函数取得结果中指定字段的字段名。
- mysql_field_len() 函数返回指定字段的长度。
- mysql_field_flags() 函数从结果中取得和指定字段关联的标志。
- mysql_fetch_row() 函数从结果集中取得一行作为数字数组。
- mysql_fetch_object() 函数从结果集(记录集)中取得一行作为对象。
- mysql_fetch_lengths() 函数取得一行中每个字段的内容的长度。
- mysql_fetch_field() 函数从结果集中取得列信息并作为对象返回。
- mysql_fetch_assoc() 函数从结果集中取得一行作为关联数组。
- mysql_fetch_array() 函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有
- mysql_error() 函数返回上一个 MySQL 操作产生的文本错误信息。
- mysql_errno() 函数返回上一个 MySQL 操作中的错误信息的数字编码。
- mysql_db_name() 函数取得 mysql_list_dbs() 调用所返回的数据库名。
- mysql_data_seek() 函数移动内部结果的指针。
- mysql_connect() 函数打开非持久的 MySQL 连接。
- mysql_close() 函数关闭非持久的 MySQL 连接。
- mysql_client_encoding() 函数返回当前连接的字符集的名称。
- mysql_affected_rows() 函数返回前一次 MySQL 操作所影响的记录行数。
- tanh() 函数返回双曲正切。
- tan() 函数返回正切。
- srand() 函数播下随机数发生器种子。
- sqrt() 函数返回一个数的平方根。
- sinh() 函数返回一个数的双曲正弦。
- sin() 函数返回一个数的正弦。
- round() 函数对浮点数进行四舍五入。
- rand() 函数返回随机整数。
- rad2deg() 函数把弧度数转换为角度数。
- pow() 函数返回 x 的 y 次方。
- pi() 函数返回圆周率的值。
- octdec() 函数把八进制转换为十进制。
- mt_srand() 播种 Mersenne Twister 随机数生成器。
- mt_rand() 使用 Mersenne Twister 算法返回随机整数。
- mt_getrandmax() 显示随机数的最大可能值。
- min() 返回最小值。
- max() 返回最大值。
- log1p() 以返回 log( + x),甚至当 x 的值接近零也能计算出准确结果。
- log10() 以 为底的对数。
- log() 返回自然对数。
- lcg_value() 组合线性同余发生器。
- is_nan() 判断是否为合法数值。
- is_infinite() 判断是否为无限值。
- is_finite() 函数判断是否为有限值。
- hypot() 函数计算一直角三角形的斜边长度。
- hexdec() 函数把十六进制转换为十进制。
- fmod() 函数显示随机数最大的可能值。
- fmod() 函数返回除法的浮点数余数。
- floor() 函数向下舍入为最接近的整数。
- expm1() 函数返回 exp(x) - ,甚至当 number 的值接近零也能计算出准确结果。
- exp() 函数计算 e 的指数。
- deg2rad() 函数将角度转换为弧度。
- decoct() 函数把十进制转换为八进制。
- dechex() 函数把十进制转换为十六进制。
- decbin() 函数把十进制转换为二进制。
- cosh() 函数返回一个数的双曲余弦。
- cos() 函数返回一个数的余弦。
- ceil() 函数向上舍入为最接近的整数。
- bindec() 函数把二进制转换为十进制。
- base_convert() 函数在任意进制之间转换数字。
- atanh() 函数返回一个角度的反双曲正切。
- atan() 和 atan2() 和 atan2() 函数
PHP String 函数
- atan() 和 atan2() 和 atan2() 函数
- asinh() 函数返回一个数的反双曲正弦。
- asin() 函数返回不同数值的反正弦,返回的结果是介于 -PI/ 与 PI/ 之间的弧度值。
- acosh() 函数返回一个数的反双曲余弦。
- acos() 函数返回一个数的反余弦。
- abs() 函数返回一个数的绝对值。
- mail() 函数允许您从脚本中直接发送电子邮件。
- libxml_use_internal_errors() 函数禁用标准的 libxml 错误,并启用用户错误处理。
- libxml_get_last_error() 函数从 libxml 错误缓冲中获取最后一个错误。
- libxml_get_errors() 函数从 libxml 错误缓冲中获取错误。
- libxml_clear_errors() 函数清空 libxml 错误缓冲。
- setrawcookie() 函数不对 cookie 值进行 URL 编码,发送一个 HTTP cookie。
- setcookie() 函数向客户端发送一个 HTTP cookie。
- headers_sent() 函数检查 HTTP 报头是否发送/已发送到何处。
- headers_list() 函数返回已发送的(或待发送的)响应头部的一个列表。
- header() 函数向客户端发送原始的 HTTP 报头。
- ftp_systype() 函数返回远程 FTP 服务器的系统类型标识符。
- ftp_ssl_connect() 函数打开一个安全的 SSL-FTP 连接。
- ftp_size() 函数返回指定文件的大小。
- ftp_site() 函数向服务器发送 SITE 命令。
- ftp_set_option() 函数设置各种 FTP 运行时选项。
- ftp_rmdir() 函数删除一个目录。
- ftp_rename() 函数更改 FTP 服务器上的文件或目录名。
- ftp_rawlist() 函数返回指定目录中文件的详细列表。
- ftp_raw() 函数向 FTP 服务器发送一个 raw 命令。
- ftp_quit() 函数关闭 FTP 连接。
- ftp_pwd() 函数返回当前目录名。
- ftp_put() 函数把文件上传到服务器。
- ftp_pasv() 函数把被动模式设置为打开或关闭。
- ftp_nlist() 函数返回指定目录的文件列表。
- ftp_nb_put() 函数把文件上传到服务器 (non-blocking)。
- ftp_nb_get() 函数从 FTP 服务器上获取文件并写入本地文件 (non-blocking)。
- ftp_nb_fput() 函数上传一个已打开的文件,并在 FTP 服务器上把它保存为文件 (non-blocking)。
- ftp_nb_fget() 函数从 FTP 服务器上下载一个文件并保存到本地已经打开的一个文件中 (non-blocking)。
- ftp_nb_continue() 函数连续获取 / 发送文件。
- ftp_mkdir() 函数在 FTP 服务器上建立新目录。
- ftp_mdtm() 函数返回指定文件的最后修改时间。
- ftp_login() 函数登录 FTP 服务器。
- ftp_get() 函数从 FTP 服务器上下载一个文件。
- ftp_get_option() 函数返回当前 FTP 连接的各种不同的选项设置。
- ftp_fput() 函数上传一个已经打开的文件到 FTP 服务器。
- ftp_fget() 函数从 FTP 服务器上下载一个文件并保存到本地一个已经打开的文件中。
- ftp_exec() 函数请求在 FTP 服务器上执行一个程序或命令。
- ftp_delete() 函数删除 FTP 服务器上的一个文件。
- ftp_connect() 函数建立一个新的 FTP 连接。
- ftp_close() 函数关闭 FTP 连接。
- ftp_chmod() 函数设置 FTP 服务器上指定文件的权限。
- ftp_chdir() 函数改变 FTP 服务器上的当前目录。
- ftp_cdup() 函数把当前目录改变为 FTP 服务器上的父目录。
- ftp_alloc() 函数为要上传到 FTP 服务器的文件分配空间。
- filter_var() 函数通过指定的过滤器过滤变量。
- filter_var_array() 函数获取多项变量,并进行过滤。
- filter_list() 函数返回包含所有得到支持的过滤器的一个数组。
- filter_input_array() 函数从脚本外部获取多项输入,并进行过滤。
- filter_input() 函数从脚本外部获取输入,并进行过滤。
- filter_id() 函数返回指定过滤器的 ID 号。
- filter_has_var() 函数检查是否存在指定输入类型的变量。
- unlink() 函数删除文件。
- umask() 函数改变当前的 umask。
- touch() 函数设置指定文件的访问和修改时间。
- tmpfile() 函数以读写(w+)模式建立一个具有唯一文件名的临时文件。
- tempnam() 函数创建一个具有唯一文件名的临时文件。
- symlink() 函数创建符号连接。
- stat() 函数返回关于文件的信息。
- set_file_buffer() 函数设置打开文件的缓冲大小。
- rmdir() 函数删除空的目录。
- rewind() 函数将文件指针的位置倒回文件的开头。
- rename() 函数重命名文件或目录。
- realpath() 函数返回绝对路径。
- readlink() 函数返回符号连接指向的目标。
- readfile() 函数输出一个文件。
- popen() 函数打开进程文件指针。
- pclose() 函数关闭由 popen() 打开的管道。
- pathinfo() 函数以数组的形式返回文件路径的信息。
- parse_ini_file() 函数解析一个配置文件,并以数组的形式返回其中的设置。
- move_uploaded_file() 函数将上传的文件移动到新位置。
- mkdir() 函数创建目录。
- lstat() 函数返回关于文件或符号连接的信息。
- linkinfo() 函数返回连接的信息。
- link() 函数建立一个硬连接。
- is_writeable() 函数判断指定的文件是否可写。
- is_writable() 函数判断指定的文件是否可写。
- is_uploaded_file() 函数判断指定的文件是否是通过 HTTP POST 上传的。
- is_readable() 函数判断指定文件名是否可读。
- is_link() 函数判断指定文件名是否为一个符号连接。
- is_file() 函数检查指定的文件名是否是正常的文件。
- is_executable() 函数检查指定的文件是否可执行。
- is_dir() 函数检查指定的文件是否是目录。
- glob() 函数返回匹配指定模式的文件名或目录。
- fwrite() 函数写入文件(可安全用于二进制文件)。
- ftruncate() 函数把文件截断到指定的长度。
- ftell() 函数在打开文件中的当前位置。
- fstat() 函数返回关于打开文件的信息。
- fseek() 函数在打开的文件中定位。
- fscanf() 函数根据指定的格式对来自打开的文件的输入进行解析。
- fread() 函数读取文件(可安全用于二进制文件)。
- fputs() 函数写入文件(可安全用于二进制文件)。
- fputcsv() 函数将行格式化为 CSV 并写入一个打开的文件。
- fpassthru() 函数输出文件指针处的所有剩余数据。
- fopen() 函数打开文件或者 URL。
- fnmatch() 函数根据指定的模式来匹配文件名或字符串。
- flock() 函数锁定或释放文件。
- filetype() 函数返回指定文件或目录的类型。
- filesize() 函数返回指定文件的大小。
- fileperms() 函数返回文件或目录的权限。
- fileowner() 函数返回文件的所有者。
- filemtime() 函数返回文件内容上次的修改时间。
- fileinode() 函数返回文件的 inode 编号。
- filegroup() 函数返回指定文件的组 ID。
- filectime() 函数返回指定文件的上次 inode 修改时间。
- fileatime() 函数返回指定文件的上次访问时间。
- file_put_contents() 函数把一个字符串写入文件中。
- file_get_contents() 函数把整个文件读入一个字符串中。
- file_exists() 函数检查文件或目录是否存在。
- file() 函数把整个文件读入一个数组中。
- fgetss() 函数从打开的文件中读取一行并过滤掉 HTML 和 PHP 标记。
- fgets() 函数从文件指针中读取一行。
- fgetcsv() 函数从文件指针中读入一行并解析 CSV 字段。
- fgetc() 函数从文件指针中读取一个字符。
- fflush() 函数将缓冲内容输出到文件。
- feof() 函数检测是否已到达文件末尾 (eof)。
- fclose() 函数关闭一个打开文件。
- diskfreespace() 函数返回目录中的可用空间。该函数是 disk_free_space() 函数的别名。
- disk_total_space() 函数返回指定目录的磁盘总大小。
- disk_free_space() 函数返回目录中的可用空间
- dirname() 函数返回路径中的目录部分。
- clearstatcache() 函数拷贝文件。
- clearstatcache() 函数清除文件状态缓存。
- chown() 函数改变指定文件的所有者。
- chmod() 函数改变文件模式。
- chgrp() 函数改变文件所属的组。
- basename() 函数返回路径中的文件名部分。
- set_exception_handler() handler() 函数
PHP Math 函数
- set_exception_handler() 函数设置用户自定义的异常处理函数。
- set_error_handler() 函数设置用户自定义的错误处理函数。
- restore_exception_handler() 函数恢复之前的异常处理程序,该程序是由 set_exception_handler() 函数改变的。
- restore_error_handler() 函数恢复之前的错误处理程序,该程序是由 set_error_handler() 函数改变的。
- error_reporting() 设置 PHP 的报错级别并返回当前级别。
- error_log() 函数向服务器错误记录、文件或远程目标发送一个错误。
- error_get_last() 函数获取最后发生的错误。
- debug_print_backtrace() 函数输出 backtrace。
- debug_backtrace() cktrace() 函数
- scandir() 函数返回一个数组,其中包含指定路径中的文件和目录。
- rewinddir() 函数重置由 opendir() 打开的目录句柄。
- readdir() 函数返回由 opendir() 打开的目录句柄中的条目。
- opendir() 函数打开一个目录句柄,可由 closedir(),readdir() 和 rewinddir() 使用。
- getcwd() 函数返回当前目录。
- closedir() 函数关闭由 opendir() 函数打开的目录句柄。
- dir() 函数打开一个目录句柄,并返回一个对象。这个对象包含三个方法:read() , rewind() 以及 close()。
- chroot() 函数把当前进程的根目录改变为指定的目录。
- chdir() 函数把当前的目录改变为指定的目录。
- time() 函数返回当前时间的 Unix 时间戳。
- strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。
- strptime() 函数解析由 strftime() 生成的日期/时间。
- strftime() 函数根据区域设置格式化本地时间/日期。
- mktime() 函数返回一个日期的 Unix 时间戳。
- microtime() 函数返回当前 Unix 时间戳和微秒数。
- localtime() 函数返回本地时间(一个数组)。
- idate() 函数将本地时间/日期格式化为整数。
- gmstrftime() 函数根据本地区域设置格式化 GMT/UTC 时间/日期。
- gmmktime() 函数取得 GMT 日期的 UNIX 时间戳。
- gmdate() 函数格式化 GMT/UTC 日期/时间。
- gettimeofday() 函数返回一个包含当前时间信息的数组。
- getdate() 函数取得日期/时间信息。
- date() 函数格式化一个本地时间/日期。
- date_sunset() 函数返回指定的日期与地点的日落时间。
- date_sunrise() 函数返回指定的日期与地点的日出时间。
- date_default_timezone_set() 函数设置用在脚本中所有日期/时间函数的默认时区。
- date_default_timezone_get() 函数返回脚本中所有日期时间函数所使用的默认时区。
- checkdate() 函数验证一个格里高里日期。
- UnixToJD() 函数把 Unix 时间戳转换为儒略日计数。
- JulianToJD() 函数把儒略历转换为儒略日计数。
- JewishToJD() 函数把犹太历法转换为儒略日计数。
- JDToUnix() 函数把儒略日计数转换为 Unix 时间戳。
- JDToGregorian() lian() 函数
- PHP Array 函数
- JDToGregorian() wish() 函数
- JDToGregorian() 函数把儒略日计数转换为格利高里历法。
- JDToFrench() 函数把儒略日计数转换为法国共和国历法。
- JDMonthName() 函数返回指定历法的月份字符串。
- JDDayOfWeek() 函数返回日期在周几。
- GregorianToJD() 函数将格利高里历法转换成为儒略日计数。
- FrenchToJD() 函数将法国共和历法转换成为儒略日计数。
- easter_days() 函数返回指定年份的复活节与 月 日之间的天数。
- easter_date() 函数返回指定年份的复活节午夜的 Unix 时间戳。
- cal_to_jd() 函数把指定的日期转换为儒略日计数。
- cal_info() 函数返回一个数组,其中包含了关于给定历法的信息。
- cal_from_jd() 函数把儒略日计数转换为指定历法的日期。
- cal_days_in_month() 函数针对指定的年份和日历,返回一个月中的天数。
- usort() 函数使用用户自定义的函数对数组排序。
- uksort() 函数使用用户自定义的比较函数按照键名对数组排序,并保持索引关系。
- uasort() 函数使用用户自定义的比较函数对数组排序,并保持索引关联(不为元素分配新的键)。
- sort() 函数按升序对给定数组的值排序。
- sizeof() 函数计算数组中的单元数目或对象中的属性个数。
- shuffle() 函数把数组中的元素按随机顺序重新排列。
- rsort() 函数对数组的元素按照键值进行逆向排序。与 arsort() 的功能基本相同。
- reset() 函数把数组的内部指针指向第一个元素,并返回这个元素的值。
- range() 函数创建并返回一个包含指定范围的元素的数组。
- prev() HP prev() 函数
PHP Error 和 Logging 函数
- pos() 函数是 current() 函数 的别名。它可返回数组中当前元素的值。
- next() 函数把指向当前元素的指针移动到下一个元素的位置,并返回当前元素的值。
- natsort() 函数用自然顺序算法对给定数组中的元素排序。
- natcasesort() 函数用不区分大小写的自然顺序算法对给定数组中的元素排序。
- list() 函数用数组中的元素为一组变量赋值。
- ksort() 函数按照键名对数组排序,为数组值保留原来的键。
- krsort() 函数将数组按照键逆向排序,为数组值保留原来的键。
- key() 函数返回数组内部指针当前指向元素的键名。
- in_array() 函数在数组中搜索给定的值。
- extract() extract() 函数
- end() 函数将数组内部指针指向最后一个元素,并返回该元素的值(如果成功)。
- each() 函数生成一个由数组当前内部指针所指向的元素的键名和键值组成的数组,并把内部指针向前移动。
- current() 函数返回数组中的当前元素(单元)。
- count() 函数计算数组中的单元数目或对象中的属性个数。
- compact() 函数创建一个由参数所带变量组成的数组。如果参数中存在数组,该数组中变量的值也会被获取。
- asort() 函数对数组进行排序并保持索引关系。主要用于对那些单元顺序很重要的结合数组进行排序。
- arsort() 函数对数组进行逆向排序并保持索引关系。主要用于对那些单元顺序很重要的结合数组进行排序。
- array_walk_recursive() cursive() 函数
- array_walk() 函数对数组中的每个元素应用回调函数。如果成功则返回 TRUE,否则返回 FALSE。
- array_values() 函数返回一个包含给定数组中所有键值的数组,但不保留键名。
- array_unshift() 函数在数组开头插入一个或多个元素。
- array_unique() 函数移除数组中的重复的值,并返回结果数组。
- array_uintersect_assoc() 函数带索引检查计算数组的交集,用回调函数比较数据。
- array_uintersect() 函数计算数组的交集,用回调函数比较数据。
- array_udiff_uassoc() 函数返回 array1 数组中存在但其它数组中都不存在的部分。返回的数组中键名保持不变。
- array_udiff_assoc() 函数返回 array1 中存在但其它数组中都不存在的部分。
- array_udiff() 函数返回一个数组,该数组包括了所有在被比较数组中,但是不在任何其它参数数组中的值,键名保留不变。
- array_sum() 函数返回数组中所有值的总和。
- array_splice() 函数与 array_slice() 函数类似,选择数组中的一系列元素,但不返回,而是删除它们并用其它值代替。
- array_slice() 函数在数组中根据条件取出一段值,并返回。
- array_shift() 函数删除数组中的第一个元素,并返回被删除元素的值。
- array_search() 函数与 in_array() 一样,在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false。
- array_reverse() 函数将原数组中的元素顺序翻转,创建新的数组并返回。如果第二个参数指定为 true,则元素的键名保持不变,否则键名将丢失。
- array_reduce() 函数用回调函数迭代地将数组简化为单一的值。如果指定第三个参数,则该参数将被当成是数组中的第一个值来处理,或者如果数组为空的话就作为最终返回值。
- array_rand() 函数从数组中随机选出一个或多个元素,并返回。
- array_push() 函数向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度。
- array_product() 函数计算并返回数组中所有值的乘积。
- array_pop() 函数删除数组中的最后一个元素。
- array_pad() 函数向一个数组插入带有指定值的指定数量的元素。
- array_multisort() 函数对多个数组或多维数组进行排序。
- array_merge_recursive() 函数与 array_merge() 函数 一样,将一个或多个数组的元素的合并起来,一个数组中的值附加在前一个数组的后面。并返回作为结果的数组。
- array_merge() 函数把两个或多个数组合并为一个数组。
- array_map() 函数返回用户自定义函数作用后的数组。回调函数接受的参数数目应该和传递给 array_map() 函数的数组数目一致。
- array_keys() 函数返回包含数组中所有键名的一个新数组。
- array_key_exists() 函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 false。
- array_intersect_ukey() 函数用回调函数比较键名来计算数组的交集。
- array_intersect_uassoc() 函数使用用户自定义的回调函数计算数组的交集,用回调函数比较索引。
- array_intersect_key() 函数使用键名比较计算数组的交集。
- array_intersect_assoc() 函数返回两个或多个数组的交集数组。
- array_intersect() 函数返回两个或多个数组的交集数组。
- array_flip() 函数返回一个反转后的数组,如果同一值出现了多次,则最后一个键名将作为它的值,所有其他的键名都将丢失。
- array_filter() 函数用回调函数过滤数组中的元素,如果自定义过滤函数返回 true,则被操作的数组的当前值就会被包含在返回的结果数组中, 并将结果组成一个新的数组。如果原数组是一个关联数组,键名保持不变。
- array_fill() 函数用给定的值填充数组,返回的数组有 number 个元素,值为 value。返回的数组使用数字索引,从 start 位置开始并递增。如果 number 为 或小于 ,就会出错。
- array_diff_ukey() 返回一个数组,该数组包括了所有出现在 array1 中但是未出现在任何其它参数数组中的键名的值。注意关联关系保留不变。与 array_diff() 不同的是,比较是根据键名而不是值来进行的。
- array_diff_uassoc() 函数使用用户自定义的回调函数 (callback) 做索引检查来计算两个或多个数组的差集。返回一个数组,该数组包括了在 array1 中但是不在任何其他参数数组中的值。
- array_diff_key() 函数返回一个数组,该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的键。
- array_diff_assoc() 函数返回两个数组的差集数组。该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的键和值。
- array_diff() 函数返回两个数组的差集数组。该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的键值。
- array_count_values() 函数用于统计数组中所有值出现的次数。
- array_combine() 函数通过合并两个数组来创建一个新数组,其中的一个数组是键名,另一个数组的值为键值。
- array_chunk() 函数把一个数组分割为新的数组块。
- array_change_key_case() 函数将数组的所有的 KEY 都转换为大写或小写。
- array() 创建数组,带有键和值。如果在规定数组时省略了键,则生成一个整数键,这个 key 从 开始,然后以 进行递增。
PHP Array 函数
1、截取UTF-8编码下字符串的函数
- /**
- * 截取UTF-8编码下字符串的函数
- *
- * @param string $str 被截取的字符串
- * @param int $length 截取的长度
- * @param bool $append 是否附加省略号
- *
- * @return string
- */
- function sub_str($str, $length = , $append = true)
- {
- $str = trim($str);//去掉前后的空格
- $strlength = strlen($str);
- if ($length == || $length >= $strlength)
- {
- return $str;
- }
- elseif ($length < )
- {
- $length = $strlength + $length;
- if ($length < )
- {
- $length = $strlength;
- }
- }
- if (function_exists('mb_substr'))
- {
- $newstr = mb_substr($str, , $length, EC_CHARSET);
- }
- elseif (function_exists('iconv_substr'))
- {
- $newstr = iconv_substr($str, , $length, EC_CHARSET);
- }
- else
- {
- //$newstr = trim_right(substr($str, 0, $length));
- $newstr = substr($str, , $length);
- }
- if ($append && $str != $newstr)
- {
- $newstr .= '...';
- }
- return $newstr;
- }
2、获得用户的真实IP地址
- /**
- * 获得用户的真实IP地址
- *
- * @access public
- * @return string
- */
- function real_ip()
- {
- static $realip = NULL;
- if ($realip !== NULL)
- {
- return $realip;
- }
- if (isset($_SERVER))
- {
- if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
- {
- $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
- /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
- foreach ($arr AS $ip)
- {
- $ip = trim($ip);
- if ($ip != 'unknown')
- {
- $realip = $ip;
- break;
- }
- }
- }
- elseif (isset($_SERVER['HTTP_CLIENT_IP']))
- {
- $realip = $_SERVER['HTTP_CLIENT_IP'];
- }
- else
- {
- if (isset($_SERVER['REMOTE_ADDR']))
- {
- $realip = $_SERVER['REMOTE_ADDR'];
- }
- else
- {
- $realip = '0.0.0.0';
- }
- }
- }
- else
- {
- if (getenv('HTTP_X_FORWARDED_FOR'))
- {
- $realip = getenv('HTTP_X_FORWARDED_FOR');
- }
- elseif (getenv('HTTP_CLIENT_IP'))
- {
- $realip = getenv('HTTP_CLIENT_IP');
- }
- else
- {
- $realip = getenv('REMOTE_ADDR');
- }
- }
- preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
- $realip = !empty($onlineip[]) ? $onlineip[] : '0.0.0.0';
- return $realip;
- }
3、计算字符串的长度(汉字按照两个字符计算)
- /**
- * 计算字符串的长度(汉字按照两个字符计算)
- *
- * @param string $str 字符串
- *
- * @return int
- */
- function str_len($str)
- {
- $length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));
- if ($length)
- {
- return strlen($str) - $length + intval($length / ) * ;
- }
- else
- {
- return strlen($str);
- }
- }
4、获得用户操作系统的换行符
- /**
- * 获得用户操作系统的换行符
- *
- * @access public
- * @return string
- */
- function get_crlf()
- {
- /* LF (Line Feed, 0x0A, \N) 和 CR(Carriage Return, 0x0D, \R) */
- if (stristr($_SERVER['HTTP_USER_AGENT'], 'Win'))
- {
- $the_crlf = '\r\n';
- }
- elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'Mac'))
- {
- $the_crlf = '\r'; // for old MAC OS
- }
- else
- {
- $the_crlf = '\n';
- }
- return $the_crlf;
- }
5、邮件发送
- /**
- * 邮件发送
- *
- * @param: $name[string] 接收人姓名
- * @param: $email[string] 接收人邮件地址
- * @param: $subject[string] 邮件标题
- * @param: $content[string] 邮件内容
- * @param: $type[int] 0 普通邮件, 1 HTML邮件
- * @param: $notification[bool] true 要求回执, false 不用回执
- *
- * @return boolean
- */
- function send_mail($name, $email, $subject, $content, $type = , $notification=false)
- {
- /* 如果邮件编码不是EC_CHARSET,创建字符集转换对象,转换编码 */
- if ($GLOBALS['_CFG']['mail_charset'] != EC_CHARSET)
- {
- $name = ecs_iconv(EC_CHARSET, $GLOBALS['_CFG']['mail_charset'], $name);
- $subject = ecs_iconv(EC_CHARSET, $GLOBALS['_CFG']['mail_charset'], $subject);
- $content = ecs_iconv(EC_CHARSET, $GLOBALS['_CFG']['mail_charset'], $content);
- $shop_name = ecs_iconv(EC_CHARSET, $GLOBALS['_CFG']['mail_charset'], $GLOBALS['_CFG']['shop_name']);
- }
- $charset = $GLOBALS['_CFG']['mail_charset'];
- /**
- * 使用mail函数发送邮件
- */
- if ($GLOBALS['_CFG']['mail_service'] == && function_exists('mail'))
- {
- /* 邮件的头部信息 */
- $content_type = ($type == ) ? 'Content-Type: text/plain; charset=' . $charset : 'Content-Type: text/html; charset=' . $charset;
- $headers = array();
- $headers[] = 'From: "' . '=?' . $charset . '?B?' . base64_encode($shop_name) . '?='.'" <' . $GLOBALS['_CFG']['smtp_mail'] . '>';
- $headers[] = $content_type . '; format=flowed';
- if ($notification)
- {
- $headers[] = 'Disposition-Notification-To: ' . '=?' . $charset . '?B?' . base64_encode($shop_name) . '?='.'" <' . $GLOBALS['_CFG']['smtp_mail'] . '>';
- }
- $res = @mail($email, '=?' . $charset . '?B?' . base64_encode($subject) . '?=', $content, implode("\r\n", $headers));
- if (!$res)
- {
- $GLOBALS['err'] ->add($GLOBALS['_LANG']['sendemail_false']);
- return false;
- }
- else
- {
- return true;
- }
- }
- /**
- * 使用smtp服务发送邮件
- */
- else
- {
- /* 邮件的头部信息 */
- $content_type = ($type == ) ?
- 'Content-Type: text/plain; charset=' . $charset : 'Content-Type: text/html; charset=' . $charset;
- $content = base64_encode($content);
- $headers = array();
- $headers[] = 'Date: ' . gmdate('D, j M Y H:i:s') . ' +0000';
- $headers[] = 'To: "' . '=?' . $charset . '?B?' . base64_encode($name) . '?=' . '" <' . $email. '>';
- $headers[] = 'From: "' . '=?' . $charset . '?B?' . base64_encode($shop_name) . '?='.'" <' . $GLOBALS['_CFG']['smtp_mail'] . '>';
- $headers[] = 'Subject: ' . '=?' . $charset . '?B?' . base64_encode($subject) . '?=';
- $headers[] = $content_type . '; format=flowed';
- $headers[] = 'Content-Transfer-Encoding: base64';
- $headers[] = 'Content-Disposition: inline';
- if ($notification)
- {
- $headers[] = 'Disposition-Notification-To: ' . '=?' . $charset . '?B?' . base64_encode($shop_name) . '?='.'" <' . $GLOBALS['_CFG']['smtp_mail'] . '>';
- }
- /* 获得邮件服务器的参数设置 */
- $params['host'] = $GLOBALS['_CFG']['smtp_host'];
- $params['port'] = $GLOBALS['_CFG']['smtp_port'];
- $params['user'] = $GLOBALS['_CFG']['smtp_user'];
- $params['pass'] = $GLOBALS['_CFG']['smtp_pass'];
- if (empty($params['host']) || empty($params['port']))
- {
- // 如果没有设置主机和端口直接返回 false
- $GLOBALS['err'] ->add($GLOBALS['_LANG']['smtp_setting_error']);
- return false;
- }
- else
- {
- // 发送邮件
- if (!function_exists('fsockopen'))
- {
- //如果fsockopen被禁用,直接返回
- $GLOBALS['err']->add($GLOBALS['_LANG']['disabled_fsockopen']);
- return false;
- }
- include_once(ROOT_PATH . 'includes/cls_smtp.php');
- static $smtp;
- $send_params['recipients'] = $email;
- $send_params['headers'] = $headers;
- $send_params['from'] = $GLOBALS['_CFG']['smtp_mail'];
- $send_params['body'] = $content;
- if (!isset($smtp))
- {
- $smtp = new smtp($params);
- }
- if ($smtp->connect() && $smtp->send($send_params))
- {
- return true;
- }
- else
- {
- $err_msg = $smtp->error_msg();
- if (empty($err_msg))
- {
- $GLOBALS['err']->add('Unknown Error');
- }
- else
- {
- if (strpos($err_msg, 'Failed to connect to server') !== false)
- {
- $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['smtp_connect_failure'], $params['host'] . ':' . $params['port']));
- }
- else if (strpos($err_msg, 'AUTH command failed') !== false)
- {
- $GLOBALS['err']->add($GLOBALS['_LANG']['smtp_login_failure']);
- }
- elseif (strpos($err_msg, 'bad sequence of commands') !== false)
- {
- $GLOBALS['err']->add($GLOBALS['_LANG']['smtp_refuse']);
- }
- else
- {
- $GLOBALS['err']->add($err_msg);
- }
- }
- return false;
- }
- }
- }
- }
6、获得服务器上的 GD 版本
- /**
- * 获得服务器上的 GD 版本
- *
- * @access public
- * @return int 可能的值为0,1,2
- */
- function gd_version()
- {
- include_once(ROOT_PATH . 'includes/cls_image.php');
- return cls_image::gd_version();
- }
- if (!function_exists('file_get_contents'))
- {
- /**
- * 如果系统不存在file_get_contents函数则声明该函数
- *
- * @access public
- * @param string $file
- * @return mix
- */
- function file_get_contents($file)
- {
- if (($fp = @fopen($file, 'rb')) === false)
- {
- return false;
- }
- else
- {
- $fsize = @filesize($file);
- if ($fsize)
- {
- $contents = fread($fp, $fsize);
- }
- else
- {
- $contents = '';
- }
- fclose($fp);
- return $contents;
- }
- }
- }
- if (!function_exists('file_put_contents'))
- {
- define('FILE_APPEND', 'FILE_APPEND');
- /**
- * 如果系统不存在file_put_contents函数则声明该函数
- *
- * @access public
- * @param string $file
- * @param mix $data
- * @return int
- */
- function file_put_contents($file, $data, $flags = '')
- {
- $contents = (is_array($data)) ? implode('', $data) : $data;
- if ($flags == 'FILE_APPEND')
- {
- $mode = 'ab+';
- }
- else
- {
- $mode = 'wb';
- }
- if (($fp = @fopen($file, $mode)) === false)
- {
- return false;
- }
- else
- {
- $bytes = fwrite($fp, $contents);
- fclose($fp);
- return $bytes;
- }
- }
- }
- if (!function_exists('floatval'))
- {
- /**
- * 如果系统不存在 floatval 函数则声明该函数
- *
- * @access public
- * @param mix $n
- * @return float
- */
- function floatval($n)
- {
- return (float) $n;
- }
- }
7、文件或目录权限检查函数
- /**
- * 文件或目录权限检查函数
- *
- * @access public
- * @param string $file_path 文件路径
- * @param bool $rename_prv 是否在检查修改权限时检查执行rename()函数的权限
- *
- * @return int 返回值的取值范围为{0 <= x <= 15},每个值表示的含义可由四位二进制数组合推出。
- * 返回值在二进制计数法中,四位由高到低分别代表
- * 可执行rename()函数权限、可对文件追加内容权限、可写入文件权限、可读取文件权限。
- */
- function file_mode_info($file_path)
- {
- /* 如果不存在,则不可读、不可写、不可改 */
- if (!file_exists($file_path))
- {
- return false;
- }
- $mark = ;
- if (strtoupper(substr(PHP_OS, , )) == 'WIN')
- {
- /* 测试文件 */
- $test_file = $file_path . '/cf_test.txt';
- /* 如果是目录 */
- if (is_dir($file_path))
- {
- /* 检查目录是否可读 */
- $dir = @opendir($file_path);
- if ($dir === false)
- {
- return $mark; //如果目录打开失败,直接返回目录不可修改、不可写、不可读
- }
- if (@readdir($dir) !== false)
- {
- $mark ^= ; //目录可读 001,目录不可读 000
- }
- @closedir($dir);
- /* 检查目录是否可写 */
- $fp = @fopen($test_file, 'wb');
- if ($fp === false)
- {
- return $mark; //如果目录中的文件创建失败,返回不可写。
- }
- if (@fwrite($fp, 'directory access testing.') !== false)
- {
- $mark ^= ; //目录可写可读011,目录可写不可读 010
- }
- @fclose($fp);
- @unlink($test_file);
- /* 检查目录是否可修改 */
- $fp = @fopen($test_file, 'ab+');
- if ($fp === false)
- {
- return $mark;
- }
- if (@fwrite($fp, "modify test.\r\n") !== false)
- {
- $mark ^= ;
- }
- @fclose($fp);
- /* 检查目录下是否有执行rename()函数的权限 */
- if (@rename($test_file, $test_file) !== false)
- {
- $mark ^= ;
- }
- @unlink($test_file);
- }
- /* 如果是文件 */
- elseif (is_file($file_path))
- {
- /* 以读方式打开 */
- $fp = @fopen($file_path, 'rb');
- if ($fp)
- {
- $mark ^= ; //可读 001
- }
- @fclose($fp);
- /* 试着修改文件 */
- $fp = @fopen($file_path, 'ab+');
- if ($fp && @fwrite($fp, '') !== false)
- {
- $mark ^= ; //可修改可写可读 111,不可修改可写可读011...
- }
- @fclose($fp);
- /* 检查目录下是否有执行rename()函数的权限 */
- if (@rename($test_file, $test_file) !== false)
- {
- $mark ^= ;
- }
- }
- }
- else
- {
- if (@is_readable($file_path))
- {
- $mark ^= ;
- }
- if (@is_writable($file_path))
- {
- $mark ^= ;
- }
- }
- return $mark;
- }
- function log_write($arg, $file = '', $line = '')
- {
- if ((DEBUG_MODE & ) != )
- {
- return;
- }
- $str = "\r\n-- ". date('Y-m-d H:i:s'). " --------------------------------------------------------------\r\n";
- $str .= "FILE: $file\r\nLINE: $line\r\n";
- if (is_array($arg))
- {
- $str .= '$arg = array(';
- foreach ($arg AS $val)
- {
- foreach ($val AS $key => $list)
- {
- $str .= "'$key' => '$list'\r\n";
- }
- }
- $str .= ")\r\n";
- }
- else
- {
- $str .= $arg;
- }
- file_put_contents(ROOT_PATH . DATA_DIR . '/log.txt', $str);
- }
8、检查目标文件夹是否存在,如果不存在则自动创建该目录
- /**
- * 检查目标文件夹是否存在,如果不存在则自动创建该目录
- *
- * @access public
- * @param string folder 目录路径。不能使用相对于网站根目录的URL
- *
- * @return bool
- */
- function make_dir($folder)
- {
- $reval = false;
- if (!file_exists($folder))
- {
- /* 如果目录不存在则尝试创建该目录 */
- @umask();
- /* 将目录路径拆分成数组 */
- preg_match_all('/([^\/]*)\/?/i', $folder, $atmp);
- /* 如果第一个字符为/则当作物理路径处理 */
- $base = ($atmp[][] == '/') ? '/' : '';
- /* 遍历包含路径信息的数组 */
- foreach ($atmp[] AS $val)
- {
- if ('' != $val)
- {
- $base .= $val;
- if ('..' == $val || '.' == $val)
- {
- /* 如果目录为.或者..则直接补/继续下一个循环 */
- $base .= '/';
- continue;
- }
- }
- else
- {
- continue;
- }
- $base .= '/';
- if (!file_exists($base))
- {
- /* 尝试创建目录,如果创建失败则继续循环 */
- if (@mkdir(rtrim($base, '/'), ))
- {
- @chmod($base, );
- $reval = true;
- }
- }
- }
- }
- else
- {
- /* 路径已经存在。返回该路径是不是一个目录 */
- $reval = is_dir($folder);
- }
- clearstatcache();
- return $reval;
- }
9、获得系统是否启用了 gzip
- /**
- * 获得系统是否启用了 gzip
- *
- * @access public
- *
- * @return boolean
- */
- function gzip_enabled()
- {
- static $enabled_gzip = NULL;
- if ($enabled_gzip === NULL)
- {
- $enabled_gzip = ($GLOBALS['_CFG']['enable_gzip'] && function_exists('ob_gzhandler'));
- }
- return $enabled_gzip;
- }
10、递归方式的对变量中的特殊字符进行转义
- /**
- * 递归方式的对变量中的特殊字符进行转义
- *
- * @access public
- * @param mix $value
- *
- * @return mix
- */
- function addslashes_deep($value)
- {
- if (empty($value))
- {
- return $value;
- }
- else
- {
- return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
- }
- }
11、将对象成员变量或者数组的特殊字符进行转
- /**
- * 将对象成员变量或者数组的特殊字符进行转义
- *
- * @access public
- * @param mix $obj 对象或者数组
- * @author Xuan Yan
- *
- * @return mix 对象或者数组
- */
- function addslashes_deep_obj($obj)
- {
- if (is_object($obj) == true)
- {
- foreach ($obj AS $key => $val)
- {
- $obj->$key = addslashes_deep($val);
- }
- }
- else
- {
- $obj = addslashes_deep($obj);
- }
- return $obj;
- }
12、递归方式的对变量中的特殊字符去除转义
- /**
- * 递归方式的对变量中的特殊字符去除转义
- *
- * @access public
- * @param mix $value
- *
- * @return mix
- */
- function stripslashes_deep($value)
- {
- if (empty($value))
- {
- return $value;
- }
- else
- {
- return is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
- }
- }
13、将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
- /**
- * 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
- *
- * @access public
- * @param string $str 待转换字串
- *
- * @return string $str 处理后字串
- */
- function make_semiangle($str)
- {
- $arr = array('0' => '', '1' => '', '2' => '', '3' => '', '4' => '',
- '5' => '', '6' => '', '7' => '', '8' => '', '9' => '',
- 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
- 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
- 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
- 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
- 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
- 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
- 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
- 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
- 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
- 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
- 'y' => 'y', 'z' => 'z',
- '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
- '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
- '‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '<',
- '》' => '>',
- '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
- ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
- ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
- '”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
- ' ' => ' ');
- return strtr($str, $arr);
- }
14、 过滤用户输入的基本数据,防止script攻击
- /**
- * 过滤用户输入的基本数据,防止script攻击
- *
- * @access public
- * @return string
- */
- function compile_str($str)
- {
- $arr = array('<' => '<', '>' => '>');
- return strtr($str, $arr);
- }
15、检查文件类型
- /**
- * 检查文件类型
- *
- * @access public
- * @param string filename 文件名
- * @param string realname 真实文件名
- * @param string limit_ext_types 允许的文件类型
- * @return string
- */
- function check_file_type($filename, $realname = '', $limit_ext_types = '')
- {
- if ($realname)
- {
- $extname = strtolower(substr($realname, strrpos($realname, '.') + 1));
- }
- else
- {
- $extname = strtolower(substr($filename, strrpos($filename, '.') + 1));
- }
- if ($limit_ext_types && stristr($limit_ext_types, '|' . $extname . '|') === false)
- {
- return '';
- }
- $str = $format = '';
- $file = @fopen($filename, 'rb');
- if ($file)
- {
- $str = @fread($file, 0x400); // 读取前 1024 个字节
- @fclose($file);
- }
- else
- {
- if (stristr($filename, ROOT_PATH) === false)
- {
- if ($extname == 'jpg' || $extname == 'jpeg' || $extname == 'gif' || $extname == 'png' || $extname == 'doc' ||
- $extname == 'xls' || $extname == 'txt' || $extname == 'zip' || $extname == 'rar' || $extname == 'ppt' ||
- $extname == 'pdf' || $extname == 'rm' || $extname == 'mid' || $extname == 'wav' || $extname == 'bmp' ||
- $extname == 'swf' || $extname == 'chm' || $extname == 'sql' || $extname == 'cert'|| $extname == 'pptx' ||
- $extname == 'xlsx' || $extname == 'docx')
- {
- $format = $extname;
- }
- }
- else
- {
- return '';
- }
- }
- if ($format == '' && strlen($str) >= 2 )
- {
- if (substr($str, 0, 4) == 'MThd' && $extname != 'txt')
- {
- $format = 'mid';
- }
- elseif (substr($str, 0, 4) == 'RIFF' && $extname == 'wav')
- {
- $format = 'wav';
- }
- elseif (substr($str ,0, 3) == "\xFF\xD8\xFF")
- {
- $format = 'jpg';
- }
- elseif (substr($str ,0, 4) == 'GIF8' && $extname != 'txt')
- {
- $format = 'gif';
- }
- elseif (substr($str ,0, 8) == "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
- {
- $format = 'png';
- }
- elseif (substr($str ,0, 2) == 'BM' && $extname != 'txt')
- {
- $format = 'bmp';
- }
- elseif ((substr($str ,0, 3) == 'CWS' || substr($str ,0, 3) == 'FWS') && $extname != 'txt')
- {
- $format = 'swf';
- }
- elseif (substr($str ,0, 4) == "\xD0\xCF\x11\xE0")
- { // D0CF11E == DOCFILE == Microsoft Office Document
- if (substr($str,0x200,4) == "\xEC\xA5\xC1\x00" || $extname == 'doc')
- {
- $format = 'doc';
- }
- elseif (substr($str,0x200,2) == "\x09\x08" || $extname == 'xls')
- {
- $format = 'xls';
- } elseif (substr($str,0x200,4) == "\xFD\xFF\xFF\xFF" || $extname == 'ppt')
- {
- $format = 'ppt';
- }
- } elseif (substr($str ,0, 4) == "PK\x03\x04")
- {
- if (substr($str,0x200,4) == "\xEC\xA5\xC1\x00" || $extname == 'docx')
- {
- $format = 'docx';
- }
- elseif (substr($str,0x200,2) == "\x09\x08" || $extname == 'xlsx')
- {
- $format = 'xlsx';
- } elseif (substr($str,0x200,4) == "\xFD\xFF\xFF\xFF" || $extname == 'pptx')
- {
- $format = 'pptx';
- }else
- {
- $format = 'zip';
- }
- } elseif (substr($str ,0, 4) == 'Rar!' && $extname != 'txt')
- {
- $format = 'rar';
- } elseif (substr($str ,0, 4) == "\x25PDF")
- {
- $format = 'pdf';
- } elseif (substr($str ,0, 3) == "\x30\x82\x0A")
- {
- $format = 'cert';
- } elseif (substr($str ,0, 4) == 'ITSF' && $extname != 'txt')
- {
- $format = 'chm';
- } elseif (substr($str ,0, 4) == "\x2ERMF")
- {
- $format = 'rm';
- } elseif ($extname == 'sql')
- {
- $format = 'sql';
- } elseif ($extname == 'txt')
- {
- $format = 'txt';
- }
- }
- if ($limit_ext_types && stristr($limit_ext_types, '|' . $format . '|') === false)
- {
- $format = '';
- }
- return $format;
- }
16、对 MYSQL LIKE 的内容进行转义
- /**
- * 对 MYSQL LIKE 的内容进行转义
- *
- * @access public
- * @param string string 内容
- * @return string
- */
- function mysql_like_quote($str)
- {
- return strtr($str, array("\\\\" => "\\\\\\\\", '_' => '\_', '%' => '\%', "\'" => "\\\\\'"));
- }
17、获取服务器的ip
- /**
- * 获取服务器的ip
- *
- * @access public
- *
- * @return string
- **/
- function real_server_ip()
- {
- static $serverip = NULL;
- if ($serverip !== NULL)
- {
- return $serverip;
- }
- if (isset($_SERVER))
- {
- if (isset($_SERVER['SERVER_ADDR']))
- {
- $serverip = $_SERVER['SERVER_ADDR'];
- }
- else
- {
- $serverip = '0.0.0.0';
- }
- }
- else
- {
- $serverip = getenv('SERVER_ADDR');
- }
- return $serverip;
- }
18、自定义 header 函数,用于过滤可能出现的安全隐患
- /**
- * 自定义 header 函数,用于过滤可能出现的安全隐患
- *
- * @param string string 内容
- *
- * @return void
- **/
- function ecs_header($string, $replace = true, $http_response_code = 0)
- {
- if (strpos($string, '../upgrade/index.php') === 0)
- {
- echo '<script type="text/javascript">window.location.href="' . $string . '";</script>';
- }
- $string = str_replace(array("\r", "\n"), array('', ''), $string);
- if (preg_match('/^\s*location:/is', $string))
- {
- @header($string . "\n", $replace);
- exit();
- }
- if (empty($http_response_code) || PHP_VERSION < '4.3')
- {
- @header($string, $replace);
- }
- else
- {
- @header($string, $replace, $http_response_code);
- }
- }
- function ecs_iconv($source_lang, $target_lang, $source_string = '')
- {
- static $chs = NULL;
- /* 如果字符串为空或者字符串不需要转换,直接返回 */
- if ($source_lang == $target_lang || $source_string == '' || preg_match("/[\x80-\xFF]+/", $source_string) == 0)
- {
- return $source_string;
- }
- if ($chs === NULL)
- {
- require_once(ROOT_PATH . 'includes/cls_iconv.php');
- $chs = new Chinese(ROOT_PATH);
- }
- return $chs->Convert($source_lang, $target_lang, $source_string);
- }
- function ecs_geoip($ip)
- {
- static $fp = NULL, $offset = array(), $index = NULL;
- $ip = gethostbyname($ip);
- $ipdot = explode('.', $ip);
- $ip = pack('N', ip2long($ip));
- $ipdot[0] = (int)$ipdot[0];
- $ipdot[1] = (int)$ipdot[1];
- if ($ipdot[0] == 10 || $ipdot[0] == 127 || ($ipdot[0] == 192 && $ipdot[1] == 168) || ($ipdot[0] == 172 && ($ipdot[1] >= 16 && $ipdot[1] <= 31)))
- {
- return 'LAN';
- }
- if ($fp === NULL)
- {
- $fp = fopen(ROOT_PATH . 'includes/codetable/ipdata.dat', 'rb');
- if ($fp === false)
- {
- return 'Invalid IP data file';
- }
- $offset = unpack('Nlen', fread($fp, 4));
- if ($offset['len'] < 4)
- {
- return 'Invalid IP data file';
- }
- $index = fread($fp, $offset['len'] - 4);
- }
- $length = $offset['len'] - 1028;
- $start = unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);
- for ($start = $start['len'] * 8 + 1024; $start < $length; $start += 8)
- {
- if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip)
- {
- $index_offset = unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
- $index_length = unpack('Clen', $index{$start + 7});
- break;
- }
- }
- fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
- $area = fread($fp, $index_length['len']);
- fclose($fp);
- $fp = NULL;
- return $area;
- }
19、去除字符串右侧可能出现的乱码
- /**
- * 去除字符串右侧可能出现的乱码
- *
- * @param string $str 字符串
- *
- * @return string
- */
- function trim_right($str)
- {
- $len = strlen($str);
- /* 为空或单个字符直接返回 */
- if ($len == 0 || ord($str{$len-1}) < 127)
- {
- return $str;
- }
- /* 有前导字符的直接把前导字符去掉 */
- if (ord($str{$len-1}) >= 192)
- {
- return substr($str, 0, $len-1);
- }
- /* 有非独立的字符,先把非独立字符去掉,再验证非独立的字符是不是一个完整的字,不是连原来前导字符也截取掉 */
- $r_len = strlen(rtrim($str, "\x80..\xBF"));
- if ($r_len == 0 || ord($str{$r_len-1}) < 127)
- {
- return sub_str($str, 0, $r_len);
- }
- $as_num = ord(~$str{$r_len -1});
- if ($as_num > (1<<(6 + $r_len - $len)))
- {
- return $str;
- }
- else
- {
- return substr($str, 0, $r_len-1);
- }
- }
20、将上传文件转移到指定位置
- /**
- * 将上传文件转移到指定位置
- *
- * @param string $file_name
- * @param string $target_name
- * @return blog
- */
- function move_upload_file($file_name, $target_name = '')
- {
- if (function_exists("move_uploaded_file"))
- {
- if (move_uploaded_file($file_name, $target_name))
- {
- @chmod($target_name,0755);
- return true;
- }
- else if (copy($file_name, $target_name))
- {
- @chmod($target_name,0755);
- return true;
- }
- }
- elseif (copy($file_name, $target_name))
- {
- @chmod($target_name,0755);
- return true;
- }
- return false;
- }
21、将JSON传递的参数转码
- /**
- * 将JSON传递的参数转码
- *
- * @param string $str
- * @return string
- */
- function json_str_iconv($str)
- {
- if (EC_CHARSET != 'utf-8')
- {
- if (is_string($str))
- {
- return addslashes(stripslashes(ecs_iconv('utf-8', EC_CHARSET, $str)));
- }
- elseif (is_array($str))
- {
- foreach ($str as $key => $value)
- {
- $str[$key] = json_str_iconv($value);
- }
- return $str;
- }
- elseif (is_object($str))
- {
- foreach ($str as $key => $value)
- {
- $str->$key = json_str_iconv($value);
- }
- return $str;
- }
- else
- {
- return $str;
- }
- }
- return $str;
- }
22、循环转码成utf8内容
- /**
- * 循环转码成utf8内容
- *
- * @param string $str
- * @return string
- */
- function to_utf8_iconv($str)
- {
- if (EC_CHARSET != 'utf-8')
- {
- if (is_string($str))
- {
- return ecs_iconv(EC_CHARSET, 'utf-8', $str);
- }
- elseif (is_array($str))
- {
- foreach ($str as $key => $value)
- {
- $str[$key] = to_utf8_iconv($value);
- }
- return $str;
- }
- elseif (is_object($str))
- {
- foreach ($str as $key => $value)
- {
- $str->$key = to_utf8_iconv($value);
- }
- return $str;
- }
- else
- {
- return $str;
- }
- }
- return $str;
- }
23、获取文件后缀名,并判断是否合法
- /**
- * 获取文件后缀名,并判断是否合法
- *
- * @param string $file_name
- * @param array $allow_type
- * @return blob
- */
- function get_file_suffix($file_name, $allow_type = array())
- {
- $file_suffix = strtolower(array_pop(explode('.', $file_name)));
- if (empty($allow_type))
- {
- return $file_suffix;
- }
- else
- {
- if (in_array($file_suffix, $allow_type))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
24、 读结果缓存文件
- /**
- * 读结果缓存文件
- *
- * @params string $cache_name
- *
- * @return array $data
- */
- function read_static_cache($cache_name)
- {
- if ((DEBUG_MODE & 2) == 2)
- {
- return false;
- }
- static $result = array();
- if (!empty($result[$cache_name]))
- {
- return $result[$cache_name];
- }
- $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
- if (file_exists($cache_file_path))
- {
- include_once($cache_file_path);
- $result[$cache_name] = $data;
- return $result[$cache_name];
- }
- else
- {
- return false;
- }
- }
25、 写结果缓存文件
- /**
- * 写结果缓存文件
- *
- * @params string $cache_name
- * @params string $caches
- *
- * @return
- */
- function write_static_cache($cache_name, $caches)
- {
- if ((DEBUG_MODE & 2) == 2)
- {
- return false;
- }
- $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
- $content = "<?php\r\n";
- $content .= "\$data = " . var_export($caches, true) . ";\r\n";
- $content .= "?>";
- file_put_contents($cache_file_path, $content, LOCK_EX);
- }
最全的PHP常用函数大全的更多相关文章
- 史上最全的PHP常用函数大全,不看看你就out了(还会不断更新哦!)
纪录了PHP的一些常用函数和函数代码!不要错过了哦. PHP的一些常用函数usleep() 函数延迟代码执行若干微秒.unpack() 函数从二进制字符串对数据进行解包.uniqid() 函数基于以微 ...
- 【javascript】javascript常用函数大全
javascript函数一共可分为五类: •常规函数 •数组函数 •日期函数 •数学函数 •字符串函数 1.常规函数 javascript常规函数包括以下9个函数: ( ...
- Matplotlib库常用函数大全
Python之Matplotlib库常用函数大全(含注释) plt.savefig(‘test’, dpi = 600) :将绘制的图画保存成png格式,命名为 test plt.ylabel(‘Gr ...
- 性能测试学习第二天-----loadrunner常用函数大全及设置项
常用函数大全: 1,C语言参数转web参数 lr_save_string("aaa","param"):将字符串“aaa”或者一个字符串变量,转变成LR的参数{ ...
- MySql常用函数大全
MySql常用函数大全 MySQL数据库中提供了很丰富的函数.MySQL函数包括数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数.格式化函数等.通过这些函数,可以简化用户的操 ...
- 【转】JNI学习积累之一 ---- 常用函数大全
原文网址:http://blog.csdn.net/qinjuning/article/details/7595104 本文原创,转载请注明出处:http://blog.csdn.net/qinjun ...
- JNI学习积累之一 ---- 常用函数大全
主要资料来源: 百度文库的<JNI常用函数> . 同时对其加以了补充 . 要素 :1. 该函数大全是基于C语言方式的,对于C++方式可以直接转换 ,例如,对于生成一个jstring类型的 ...
- Oracle 常用函数大全
Oracle 11g 常用函数(Functions)详解 目录 ABS. 3 ACOS. 3 ADD_MONTHS. 4 ASCII 4 ASCIISTR. 5 ASIN.. 5 ATAN.. 5 A ...
- JNI之常用函数大全
要素 :1. 该函数大全是基于C语言方式的,对于C++方式可以直接转换 ,例如,对于生成一个jstring类型的方法转换分别如下: C编程环境中使用方法为:(*env) ->NewString ...
随机推荐
- sigaction 函数
本文主要参考<unix环境高级编程> sigaction函数的功能是检查或修改与指定信号相关联的处理动作(可同时两种操作). int sigaction(int signo,con ...
- IE如何兼容placeholder属性
在前端开发中,经常需要为input设置placeholder属性,但是placeholder是HTML5新属性,在IE10以下不兼容,那么如何完美兼容呢? 网上搜索了一下,其实也挺简单的,可以采用以下 ...
- RMQ(st)
int dp[1111][12]; int a[1111]; int n; void RMQ_init() { for(int i=1;i<=n;i++) { d ...
- ASP.NET MVC 从IHttp到页面输出
MVCHandler应该算是MVC真正开始的地方.MVCHandler实现了IHttpHandler接口,ProcessRequest便是方法入口. MVCHandler : IHttpHandler ...
- vs2010把项目资源打包成系统资源
把wav格式的音频做成系统资源,根据条件播放相应的音频 System.Media.SoundPlayer spOne = new System.Media.SoundPlayer(); ...
- Unity3D研究院之静态自动检查代码缺陷与隐患
原地址:原地址:http://www.xuanyusong.com/archives/2828 代码缺陷和代码错误的最大区别是,代码缺陷不影响游戏编译,而代码错误编译都不通过.但是代码缺陷会影响游戏发 ...
- php弱类型
此处写ctf中遇到的==和md5值为0e**的应用. 数组和字符的=== ==
- ZOJ3717 Balloon(2-SAT)
一个很玄乎的问题,但听到2-SAT之后就豁然开朗了.题目的意思是这样的,给你n个点群,每个点群里面有两个点,你要在每个点群里面选一个点,以这些点做半径为r的圆,然后r会有一个最大值,问的就是怎么选这些 ...
- Flatty Shadow在线为Icon图标生成长阴影效果。
Flatty Shadow在线为Icon图标生成长阴影效果. Flatty Shadow 彩蛋爆料直击现场 Flatty Shadow在线为Icon图标生成长阴影效果.
- POJ 2101
#include <iostream> #include <algorithm> #include <cmath> using namespace std; int ...