101.array_keys(): 返回数组所有的键,组成一个数组
 $a=array(“a”=>”Horse”,”b”=>”Cat”,”c”=>”Dog”);
 print_r(array_keys($a));

输出: 返回由键名组成的数组


102.array_values(): 返回数组中所有值,组成一个数组

输出: 返回由键值组成的数组


103.array_reverse(): 返回一个元素顺序相反的数组 元素顺序相反的一个数组,键名和键值依然匹配


104.array_count_values(): 统计数组中所有的值出现的次数
 $a=array(“Cat”,”Dog”,”Horse”,”Dog”);
 print_r(array_count_values($a));

输出: 返回数组原键值为新键名,次数为新键值


105.array_rand(): 从数组中随机抽取一个或多个元素,注意是键名!!!
 $a=array(“a”=>”Dog”,”b”=>”Cat”,”c”=>”Horse”);
 print_r(array_rand($a,1));

$a为目标数组, 1为抽取第几个元素的键名返回第1个元素的键名b


106.each(): 返回数组中当前的键/值对并将数组指针向前移动一步 调用array each ( array &$array )
在执行 each() 之后,数组指针将停留在数组中的下一个单元或者当碰到数组结尾时停留在最后一个单元。如果要再用 each 遍历数组,必须使用 reset()。
返回值: 数组中当前指针位置的键/值对并向前移动数组指针。键值对被返回为四个单元的数组,键名为0,1,key和 value。单元 0 和 key 包含有数组单元的键名,1 和 value 包含有数据。 如果内部指针越过了数组的末端,则 each() 返回 FALSE。


107.array_unique(): 删除重复值,返回剩余数组

 $a=array(“a”=>”Cat”,”b”=>”Dog”,”c”=>”Cat”);
 print_r(array_unique($a));
输入: 数组 输入: 返回无重复值数组,键名不变
数组排序:


108.sort(): 按升序对给定数组的值排序,不保留键名

 $my_array = array(“a” => “Dog”, “b” => “Cat”,
 “c” => “Horse”);
 sort($my_array);
 print_r($my_array);

输出: true/false   


109.rsort(): 对数组逆向排序,不保留键名

 

110.asort(): 对数组排序,保持索引关系

 

111.arsort(): 对数组逆向排序,保持索引关  


112.ksort(): 系按键名对数组排序  


113.krsort(): 将数组按照键逆向排序 


114.natsort(): 用自然顺序算法对数组中的元素排序

 

115.natcasesort(): 自然排序,不区分大小写


文件系统函数
116.fopen(): 打开文件或者 URL
$handle = fopen(“ftp://user:password@example.com/somefile.txt”, “w”);
调用: resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )
返回值: 如果打开失败,本函数返回 FALSE


117.fclose(): 关闭一个已打开的文件指针

$handle = fopen(‘somefile.txt’, ‘r’);
fclose($handle);
bool fclose(resource handle)

输出: 如果成功则返回 TRUE,失败则返回 FALSE


文件属性
118.file_exists(): 检查文件或目录是否存在
 $filename = ‘/path/to/foo.txt’;
 if (file_exists($filename)) {
 echo “exists”;
 } else {
 echo “does not exist”;
 }
调用: bool file_exists ( string filename ) 输入: 指定的文件或目录 输出: 存在则返回 TRUE,否则返回 FALSE


119.filesize(): 取得文件大小

$filename = ‘somefile.txt’;
echo $filename . ‘: ‘ . filesize($filename) .’bytes’;
调用: int filesize ( string $filename )
输出: 返回文件大小的字节数,如果出错返回 FALSE 并生成一条 E_WARNING 级的错误


120.is_readable(): 判断给定文件是否可读

 $filename = ‘test.txt’;
 if (is_readable($filename)) {
 echo ‘可读’;
 } else {
 echo ‘不可读’;
 }
调用: bool is_readable ( string $filename ) 输出: 如果由 filename指定的文件或目录存在并且可读则返回 TRUE


121.is_writable(): 判断给定文件是否可写

$filename = ‘test.txt’;

 if (is_writable($filename)) {

 echo ‘可写’;
 } else {
 echo ‘不可写’;
 }
调用: bool is_writable ( string $filename ) filename 参数 可以是一个允许进行是否可写检查的目录名
输出: 如果文件存在并且可写则返回 TRUE。


122.is_executable(): 判断给定文件是否可执行

 $file = ‘setup.exe’;
 if (is_executable($file)) {
 echo ‘可执行’;
 } else {
 echo ‘不可执行’;
 }
调用: bool is_executable ( string $filename ) 输出: 如果文件存在且可执行则返回 TRUE


123.filectime(): 获取文件的创建时间

$filename = ‘somefile.txt’;
echo filectime($filename);
调用: int filectime ( string $filename ) 输出: 时间以 Unix 时间戳的方式返回,如果出错则返回FALSE


124.filemtime(): 获取文件的修改时间

$filename = ‘somefile.txt’;

echo filemtime($filename);

 int filemtime ( string $filename )

输出: 返回文件上次被修改的时间,出错时返回 FALSE。时间以 Unix时间戳的方式返回


125.fileatime(): 获取文件的上次访问时间

$filename = ‘somefile.txt’;
echo fileatime($filename);
调用: int fileatime (string $filename)
输出: 返回文件上次被访问的时间, 如果出错则返回FALSE. 时间以Unix时间戳的方式返回.


126.stat(): 获取文件大部分属性值

$filename = ‘somefile.txt’;
var_dump(fileatime($filename));

调用: array stat (string $filename 输出: 返回由 filename 指定的文件的统计信息


文件操作

127.fwrite(): 写入文件

$filename = ‘test.txt’;
$somecontent = “添加这些文字到文件n”;
$handle = fopen($filename, ‘a’);
fwrite($handle, $somecontent);
fclose($handle);
调用: int fwrite ( resource handle, string string [, int length] )
输出: 把 string 的内容写入 文件指针 handle 处。如果指定了 length,当写入了length个字节或者写完了string以后,写入就会停止, 视乎先碰到哪种情况


128.fputs(): 同上


129.fread(): 读取文件

$filename = “/usr/local/something.txt”;

$handle = fopen($filename, “r”);
$contents = fread($handle, filesize($filename));
fclose($handle);
调用: string fread ( int handle, int length ) 从文件指针handle,读取最多 length 个字节


130.feof(): 检测文件指针是否到了文件结束的位置

$file = @fopen(“no_such_file”, “r”);
while (!feof($file)) {
}
fclose($file);
调用: bool feof ( resource handle ) 输出: 如果文件指针到了 EOF 或者出错时则返回TRUE,否则返回一个错误(包括 socket 超时),其它情况则返回 FALSE


131.fgets(): 从文件指针中读取一行

 $handle = @fopen(“/tmp/inputfile.txt”, “r”);
 if ($handle) {
 while (!feof($handle)) {
 $buffer = fgets($handle, 4096);
 echo $buffer;
 }
 fclose($handle);
 }
调用: string fgets ( int handle [, int length] ) 输出: 从handle指向的文件中读取一行并返回长度最多为length-1字节的字符串.碰到换行符(包括在返回值中)、EOF 或者已经读取了length -1字节后停止(看先碰到那一种情况). 如果没有指定 length,则默认为1K, 或者说 1024 字节.


132.fgetc(): 从文件指针中读取字符

 $fp = fopen(‘somefile.txt’, ‘r’);
 if (!$fp) {
 echo ‘Could not open file somefile.txt’;
 }
 while (false !== ($char = fgetc($fp))) {
 echo “$charn”;
 }
输入: string fgetc ( resource $handle ) 输出: 返回一个包含有一个字符的字符串,该字符从 handle指向的文件中得到. 碰到 EOF 则返回 FALSE.


133.file(): 把整个文件读入一个数组中

 $lines = file(‘https://www.example.com/’);
// 在数组中循环,显示 HTML 的源文件并加上行号。
 foreach ($lines as $line_num => $line) {
 echo “Line #<b>{$line_num}</b> : ” .
 htmlspecialchars($line) . “<br />n”;
 }
// 另一个例子将 web 页面读入字符串。参见 file_get_contents()。
$html = implode(”, file(‘https://www.example.com/’));
调用: array file ( string $filename [, int $use_include_path [, resource $context ]] )
输出: 数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败 file() 返回 FALSE


134.readfile(): 输出一个文件  调用: int readfile ( string $filename [, bool $use_include_path [, resource $context ]] )

输出: 读入一个文件并写入到输出缓冲。返回从文件中读入的字节数。如果出错返回 FALSE


135.file_get_contents(): 将整个文件读入一个字符串

echo file_get_contents(‘https://www.baidu.com’);

调用: string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] )   


136.file_put_contents():将一个字符串写入文件

file_put_contents(‘1.txt’,’aa’);
调用: int file_put_contents ( string $filename , string $data [, int $flags [, resource $context ]] )
输出: 该函数将返回写入到文件内数据的字节数


137.ftell(): 返回文件指针读/写的位置

$fp=fopen(‘tx.txt’,’r’);
fseek($fp,10);
echo ftell($fp);
fread($fp,4);
echo ftell($fp);
调用: int ftell ( resource $handle ) 输出: 返回由 handle 指定的文件指针的位置,也就是文件流中的偏移量


138.fseek(): 在文件指针中定位

 $fp=fopen(‘tx.txt’,’r’);
 fseek($fp,10);
 echo ftell($fp);
 fread($fp,4);
 echo ftell($fp);
调用: int fseek ( resource $handle , int $offset [, int $whence ] ) 输出: 成功则返回 0;否则返回 -1


139.rewind(): 倒回文件指针的位置

$fp=fopen(‘tx.txt’,’r’);
fseek($fp,3);
echo ftell($fp);
fread($fp,4);
rewind($fp);
echo ftell($fp);
调用: bool rewind ( resource $handle ) 返回值: 如果成功则返回 TRUE,失败则返回 FALSE


140.flock(): 轻便的执行文件锁定

 $fp=fopen(‘tx.txt’,’r’);
 flock($fp, LOCK_SH);//共享锁
 //flock($fp, LOCK_EX);//独立锁,写文件时用它打开
 //flock($fp, LOCK_NB);//附加锁
 flock($fp, LOCK_UN);//释放锁
 fclose($fp);
调用: bool flock ( int $handle , int $operation [, int &$wouldblock ] ) 输出: 如果成功则返回 TRUE,失败则返回 FALSE
目录


141.basename(): 返回路径中的文件名部分

 path = “/home/httpd/html/index.php”;
 $file = basename($path);
 $file = basename($path,”.php”);
调用: string basename ( string $path [, string $suffix ]) 输出: 给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名。如果文件名是以 suffix 结 束的,那这一部分也会被去掉


142.dirname(): 返回路径中的目录部分

 $path = “/etc/passwd”;
 $file = dirname($path);
调用: string dirname ( string $path ) 输出: 给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名


143.pathinfo(): 返回文件路径的信息

 echo ‘<pre>’;
 print_r(pathinfo(“/www/htdocs/index.html”));
 echo ‘</pre>’;
调用: mixed pathinfo ( string $path [, int $options ] ) 返回一个关联数组包含有 path 的信息


144.opendir(): 打开目录句柄

$fp=opendir(‘E:/xampp/htdocs/php/study/19’);
echo readdir($fp);
closedir($fp);
调用: resource opendir ( string $path [, resource $context ] ) 返回值: 如果成功则返回目录句柄的 resource,失败则返回FALSE


145.readdir(): 从目录句柄中读取条目

$fp=opendir(‘E:/xampp/htdocs/php/study/19’);
echo readdir($fp);
closedir($fp);
调用: string readdir ( resource $dir_handle ) 返回值: 返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回


146.closedir(): 关闭目录句柄

$fp=opendir(‘E:/xampp/htdocs/php/study/19’);
echo readdir($fp);
closedir($fp);

调用: void closedir ( resource $dir_handle ) 关闭由 dir_handle 指定的目录流。流必须之前被opendir() 所打开 


147.rewinddir() : 倒回目录句柄

 $fp=opendir(‘E:/xampp/htdocs/php/study/19’);
 echo readdir($fp).'<br />’;
 echo readdir($fp).'<br />’;
 echo readdir($fp).'<br />’;
 rewinddir($fp);
 echo readdir($fp).'<br />’;
 closedir($fp);
调用: void rewinddir ( resource $dir_handle ) 输出: 将 dir_handle 指定的目录流重置到目录的开头 148.mkdir(): 新建目录
mkdir(‘123’);
调用: bool mkdir ( string $pathname [, int $mode [, bool $recursive [, resource $context ]]] ) 输出: 尝试新建一个由 pathname 指定的目录


149.rmdir(): 删除目录

rmdir(‘123’);
调用: bool rmdir ( string $dirname ) 输出: 尝试删除 dirname 所指定的目录。目录必须是空的,而且要有相应的权限。如果成功则返回TRUE,失败则返回 FALSE


150.unlink(): 删除文件

 unlink(‘123/1.txt’);
 rmdir(‘123’);
调用: bool unlink ( string $filename ) 输出: 删除 filename 。和 Unix C 的 unlink() 函数相似。如果成功则返回 TRUE,失败则返回 FALSE


151.copy(): 拷贝文件

copy(‘index.php’,’index.php.bak’);
调用: bool copy ( string $source , string $dest ) 输出: 将文件从 source 拷贝到 dest. 如果成功则返回TRUE,失败则返回 FALSE


152.rename(): 重命名一个文件或目录

rename(‘tx.txt’,’txt.txt’);
调用: bool rename ( string $oldname , string $newname [, resource $context ] ) 输出: 如果成功则返回 TRUE,失败则返回 FALSE
文件的上传与下载


153.is_uploaded_file():判断文件是否是通过 HTTP POST上传的

 if(is_uploaded_file($_FILES[‘bus’][‘tmp_name’])){
 if( move_uploaded_file($_FILES[‘bus’][‘tmp_name’],
 $NewPath) ){
 echo ‘上传成功<br /><img src=”‘.$NewPath.'”>’;
 }else{
 exit(‘失败’);
 }
 }else{
 exit(‘不是上传文件’);
 }
调用: bool is_uploaded_file ( string $filename )


154.move_uploaded_file(): 将上传的文件移动到新位置

 if(is_uploaded_file($_FILES[‘bus’][‘tmp_name’])){
 if( move_uploaded_file($_FILES[‘bus’][‘tmp_name’],
 $NewPath) ){
 echo ‘上传成功<br /><img src=”‘.$NewPath.'”>’;
 }else{
 exit(‘失败’);
 }
 }else{
 exit(‘不是上传文件’);
 }
调用: bool move_uploaded_file ( string $filename , string
时间函数


155.time(): 返回当前的 Unix 时间戳time(); 调用: int time ( void ) 输出: 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数


156.mktime(): 取得一个日期的 Unix 时间戳

mktime(0, 0, 0, 4, 25, 2012);
调用: int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] )   156.date(): 格式化一个本地时间/日期
date(‘Y年m月d日 H:i:s’);
调用: string date ( string $format [, int $timestamp ] )
输出: 2016年09月10日 20:45:54


157.checkdate(): 验证一个格里高里日期 调用: bool checkdate ( int $month , int $day , int $year) 输出: 如果给出的日期有效则返回 TRUE,否则返回 FALSE

 if(checkdate(6,31,2012)){
 echo ‘成立’;
 }else{
 echo ‘不成立’;
 }


158.date_default_timezone_set(): 设定用于一个脚本中所有日期时间函数的默认时区

date_default_timezone_set(‘PRC’);
调用: bool date_default_timezone_set ( string $timezone_identifier)
返回值: 如果 timezone_identifier 参数无效则返回 FALSE,否则返回 TRUE。


159.getdate(): 取得日期/时间信息 调用: array getdate ([ int $timestamp ] )

输出: 返回一个根据timestamp得出的包含有日期信息的关联数组。如果没有给出时间戳则认为是当前本地时间
 $t=getdate();
 var_dump($t);


160.strtotime(): 将任何英文文本的日期时间描述解析为 Unix 时间戳

 echo strtotime(“now”);
 int strtotime ( string $time [, int $now ] )
 echo strtotime(“10 September 2000”);
 echo strtotime(“+1 day”);
 echo strtotime(“+1 week”);
 echo strtotime(“+1 week 2 days 4 hours 2 seconds”);
 echo strtotime(“next Thursday”);
 echo strtotime(“last Monday”);


161.microtime(): 返回当前 Unix 时间戳和微秒数 调用: mixed microtime ([ bool $get_as_float ] )

 $start=microtime(true);
 sleep(3);
 $stop=microtime(true);
 echo $stop-$start;
其他常用:


162.intval(): 获取变量的整数值 调用: int intval ( mixed $var [, int $base = 10 ] ) 通过使用指定的进制 base 转换(默认是十进制),返回变量 var 的 integer 数值。 intval() 不能用于 object,否则会产生 E_NOTICE 错误并返回 1。

var: 要转换成 integer 的数量值
base: 转化所使用的进制
返回值: 成功时返回 var 的 integer 值,失败时返回 0。 空的 array 返回 0,非空的 array 返回 1。
PDO类的相关函数 prepare() execute() fetch()
<?php
$driver = ‘mysql’;
$database = “dbname=CODINGGROUND”;
$dsn = “$driver:host=localhost;unix_socket=/home/cg/mysql/mysql.sock;$database”;

$username = ‘root’;
$password = ‘root’;

try {
 $conn = new PDO($dsn, $username, $password);
 echo “<h2>Database CODINGGROUND Connected<h2>”;
}catch(PDOException $e){
 echo “<h1>” . $e->getMessage() . “</h1>”;
}
$sql = ‘SELECT * FROM users’;
$stmt = $conn->prepare($sql);
$stmt->execute();

echo “<table style=’width:100%’>”;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
 echo “<tr>”;
 foreach($row as $value)
 {
 echo sprintf(“<td>%s</td>”, $value);
 }
 echo “</tr>”;
}
echo “</table>”;
?>

正则表达式-元字符
元字符及其匹配范围
d 匹配任意一个十进制数字,等价于: [0-9] D 匹配除十进制数字以外的任意数字,等价于: [^0-9] s:匹配空白字符,等价于: [nfrtv] S: 匹配除空白字符以外的任意一个字符, 等价于[^nfrtv]
w 匹配任意一个数字、字母和下划线,等价于: [0-9a-zA-Z_] W 匹配除字母、数字和下划线以外的任意字符, 等价于: [^0-9a-zA-Z_] [] 1)用来表示范围。2)匹配任意一个中括号中定义的原子   [^]: 中括号里面的^(抑扬符):表示匹配任意一个除中括号里面定义的原子


限定次数

* 匹配0次、1次或多次其前的原子, 等价于: {0,} + 匹配1次或多次其前的原子, 等价于: {1,} ? 匹配0次或1次其前的原子, 等价于: {0,1} {n} 表示其前的原子正好出现n次, 等价于: {n,} 表示其前的原子至少出现n次,最多不限制   {m,n} 表示其前的原子最少出现m次,最多出现n次


其它

. 匹配除换行符(n)以外的任意字符【windows下还匹配fr】  | 两个或多个分支选择【优先级最低】 ^ 匹配输入字符的开始位置  $ 匹配输入字符的结束位置  b 匹配词边界  B 匹配非词边界  () 1)模式单元,把多个小原子组成一个大原子。2)可以改变优先级