Heim  >  Artikel  >  Zusammenfassung von mehr als 180 gängigen PHP-Funktionen

Zusammenfassung von mehr als 180 gängigen PHP-Funktionen

小云云
小云云Original
2018-01-20 18:01:232991Durchsuche

Wir wissen, dass jeder gültige PHP-Code innerhalb einer Funktion erscheinen kann, auch in anderen Funktionen und Klassendefinitionen. In diesem Artikel teilen wir Ihnen hauptsächlich eine Zusammenfassung von mehr als 180 gängigen PHP-Funktionen mit, in der Hoffnung, jedem dabei zu helfen, PHP effizienter zu erlernen.

Mathematische Funktion

1.abs(): Finden Sie den Absolutwert

<span style="font-size: 14px;">$abs = abs(-4.2); //4.2<br></span>
  • 1

  • 1

Eingabe: Zahl
Ausgabe: Absolute Zahl

2.ceil(): Runden auf die nächste ganze Zahl

<span style="font-size: 14px;">echo ceil(9.999); // 10<br></span>
  • 1

  • 1

Ausgabe: Gleitkommazahlen werden auf ganze Zahlen gerundet

3.floor(): Tail-Removing-Methode wird auf ganze Zahlen gerundet

<span style="font-size: 14px;">    echo floor(9.999);  // 9<br></span>
  • 1

  • 1

Ausgabe: Gleitkommazahlen verwerfen direkt den Dezimalteil

4.fmod(): Gleitkommazahlen nehmen den Rest an

<span style="font-size: 14px;">    $x = 5.7;    $y = 1.3; // 两个浮点数,x>y 浮点余数<br>    $r = fmod($x, $y);    // $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7<br></span>
  • 1

  • 2

  • 3

  • 4

  • 1

  • 2

  • 3

  • 4

5.pow(): Gibt die n-te Potenz der Zahl zurück

<span style="font-size: 14px;">    echo pow(-1, 20); // 1 基础数|n次方乘方值<br></span>
  • 1

  • 1

6.round(): Gleitkomma Zahlen werden gerundet

<span style="font-size: 14px;">    echo round(1.95583, 2); // 1.96, 一个数值|保留小数点后多少位,默认为0 舍入后的结果<br></span>
  • 1

  • 1

7.sqrt(): Finden Sie die Quadratwurzel

<span style="font-size: 14px;">    echo sqrt(9); //3 被开方的数平方根<br></span>
  • 1

  • 1

8.max(): Finden der Maximalwert

<span style="font-size: 14px;">    echo max(1, 3, 5, 6, 7); // 7<br></span>
  • 1

  • 1

Mehrere Zahlen oder Arrays
Gibt den Maximalwert unter ihnen zurück

<span style="font-size: 14px;">    echo max(array(2, 4, 5)); // 5<br></span>
  • 1

  • 1

9.min( ): Finden Sie den Mindestwert

Eingabe: Mehrere Zahlen oder Arrays

Ausgabe: Geben Sie den Mindestwert unter ihnen zurück

10.mt_rand(): Weitere gute Zufallszahl

Eingabe: min|max,
Ausgabe: Zufallszahl gibt zufällig einen Wert im Bereich zurück

<span style="font-size: 14px;">    echo mt_rand(0,9);//n<br></span>
  • 1

  • 1

11.rand(): Zufallszahl
Eingabe: Minimum|Maximum,
Ausgabe: Zufallszahl gibt zufällig einen Wert innerhalb des Bereichs zurück

12.pi(): Pi-Wert abrufen

String-Funktion

Leerzeichen oder andere Zeichen entfernen:

13.trim (): Entfernen Sie Leerzeichen oder andere vordefinierte Zeichen an beiden Enden der Zeichenfolge

<span style="font-size: 14px;">    $str = "\r\nHello World!\r\n";    echo trim($str);<br></span>
  • 1

  • 2

  • 1

  • 2

输入: 目标字符串 
返回值: 清除后的字符串

14.rtrim(): 删除字符串右边的空格或其他预定义字符

<span style="font-size: 14px;">    $str = "Hello World!\n\n";    echo rtrim($str);<br></span>
  • 1

  • 2

  • 1

  • 2

15.chop(): rtrim()的别名

16.ltrim(): 删除字符串左边的空格或其他预定义字符

<span style="font-size: 14px;">    $str = "\r\nHello World!";    echo ltrim($str);<br></span>
  • 1

  • 2

  • 1

  • 2

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

<span style="font-size: 14px;">    echo dirname("c:/testweb/home.php");  //c:/testweb<br></span>
  • 1

  • 1

输入: 一个包含路径的字符串 
返回值: 返回文件路径的目录部分

字符串生成与转化:  

18.str_pad(): 把字符串填充为指定的长度

<span style="font-size: 14px;">    $str = "Hello World";    echo str_pad($str,20,".");<br></span>
  • 1

  • 2

  • 1

  • 2

输入: 
要填充的字符串|新字符串的长度|供填充使用的字符串, 默认是空白

输出: 
完成后的字符串

19.str_repeat(): 重复使用指定字符串

<span style="font-size: 14px;">    echo str_repeat(".",13); // 要重复的字符串|字符串将被重复的次数13个点<br></span>
  • 1

  • 1

20.str_split(): 把字符串分割到数组中

<span style="font-size: 14px;">print_r(str_split("Hello"));<br></span>
  • 1

  • 1

输入: 要分割的字符串|每个数组元素的长度,默认1

输出: 拆分后的字符串数组

21.strrev(): 反转字符串

<span style="font-size: 14px;">    echo strrev("Hello World!"); // !dlroW olleH<br></span>
  • 1

  • 1

输出: 目标字符串颠倒顺序后的字符串 

22.wordwrap(): 按照指定长度对字符串进行折行处理

<span style="font-size: 14px;">    $str = "An example on a long word is: Supercalifragulistic";    echo wordwrap($str,15);<br></span>
  • 1

  • 2

  • 1

  • 2

输入: 目标字符串|最大宽数

输出: 折行后的新字符串

23.str_shuffle(): 随机地打乱字符串中所有字符

<span style="font-size: 14px;">    echo str_shuffle("Hello World");<br></span>
  • 1

  • 1

输入: 目标字符串顺序 
输出: 打乱后的字符串

24.parse_str(): 将字符串解析成变量

<span style="font-size: 14px;">    parse_str("id=23&name=John%20Adams", $myArray);<br>    print_r($myArray);<br></span>
  • 1

  • 2

  • 1

  • 2

输入: 要解析的字符串|存储变量的数组名称

输出:

<span style="font-size: 14px;">Array(<br>[id] => 23[name] => John Adams)<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

25.number_format(): 通过千位分组来格式化数字 
输入: 
要格式化的数字|规定多少个小数|规定用作小数点的字符串|规定用作千位分隔符的字符串

输出:

<span style="font-size: 14px;">1,000,000<br>1,000,000.00<br>1.000.000,00<br></span>

大小写转换:

26.strtolower(): 字符串转为小写

<span style="font-size: 14px;">    echo strtolower("Hello WORLD!");<br></span>
  • 1

  • 1

目标字符串 
小写字符串

27.strtoupper(): 字符串转为大写

<span style="font-size: 14px;">    echo strtoupper("Hello WORLD!");<br></span>
  • 1

  • 1

输出: 大写字符串

28.ucfirst(): 字符串首字母大写

<span style="font-size: 14px;">    echo ucfirst("hello world"); // Hello world<br></span>
  • 1

  • 1

29.ucwords(): 字符串每个单词首字符转为大写

<span style="font-size: 14px;">    echo ucwords("hello world"); // Hello World<br></span>
  • 1

  • 1

html标签关联:

30.htmlentities(): 把字符转为HTML实体

<span style="font-size: 14px;">    $str = "John & 'Adams'";echo htmlentities($str, ENT_COMPAT); // John & 'Adams'<br></span>
  • 1

  • 2

  • 1

  • 2

31.htmlspecialchars(): 预定义字符转html编码

32.nl2br(): \n转义为
标签

<span style="font-size: 14px;">    echo nl2br("One line.\nAnother line.");<br></span>
  • 1

  • 1

输出: 处理后的字符串

33.strip_tags(): 剥去 HTML、XML 以及 PHP 的标签

<span style="font-size: 14px;">    echo strip_tags("Hello <b>world!</b>");  <br></span>
  • 1

  • 1

34.addcslashes():在指定的字符前添加反斜线转义字符串中字符

<span style="font-size: 14px;">    $str = "Hello, my name is John Adams.";    echo $str;    echo addcslashes($str,'m');<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

输入: 
目标字符串|指定的特定字符或字符范围

35.stripcslashes(): 删除由addcslashes()添加的反斜线

<span style="font-size: 14px;">    echo stripcslashes("Hello, \my na\me is Kai Ji\m.");    // 目标字符串 Hello, my name is Kai Jim.<br></span>
  • 1

  • 2

  • 1

  • 2

36.addslashes(): 指定预定义字符前添加反斜线

<span style="font-size: 14px;">    $str = "Who's John Adams?";echo addslashes($str);<br></span>
  • 1

  • 2

  • 1

  • 2

输出: 把目标串中的’ ” \和null进行转义处理

37.stripslashes(): 删除由addslashes()添加的转义字符

<span style="font-size: 14px;">    echo stripslashes("Who\'s John Adams?"); // 清除转义符号Who's John Adams?<br></span>
  • 1

  • 1

38.quotemeta(): 在字符串中某些预定义的字符前添加反斜线

<span style="font-size: 14px;">    $str = "Hello world. (can you hear me?)";echo quotemeta($str);    // Hello world\. \(can you hear me\?\)<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

39.chr(): 从指定的 ASCII 值返回字符

<span style="font-size: 14px;">    echo chr(052); // ASCII 值返回对应的字符<br></span>
  • 1

  • 1

40.ord(): 返回字符串第一个字符的ASCII值

<span style="font-size: 14px;">    echo ord("hello"); 字符串第一个字符的 ASCII 值<br></span>
  • 1

  • 1

字符串比较:

41.strcasecmp(): 不区分大小写比较两字符串

<span style="font-size: 14px;">    echo strcasecmp("Hello world!","HELLO WORLD!");<br></span>
  • 1

  • 1

输入: 
两个目标字符串 
输出: 
大1|等0|小 -1

42.strcmp(): 区分大小写比较两字符串

43.strncmp(): 比较字符串前n个字符,区分大小写

调用: int strncmp ( string $str1 , string $str2 , int $len) 
  
44.strncasecmp(): 比较字符串前n个字符,不区分大小写

调用: int strncasecmp ( string $str1 , string $str2 , int $len )

45.strnatcmp(): 自然顺序法比较字符串长度,区分大小写

调用: int strnatcmp ( string $str1 , string $str2 )

输入: 
目标字符串 

46.strnatcasecmp(): 自然顺序法比较字符串长度, 不区分大小写

调用: int strnatcasecmp ( string $str1 , string $str2 )

字符串切割与拼接:

47.chunk_split():将字符串分成小块

调用: str chunk_split(str $body[,int $len[,str $end]])

输入: 
$body目标字串, $len长度, $str插入结束符 
输出: 
分割后的字符串

48.strtok(): 切开字符串

调用: str strtok(str $str,str $token)

目标字符串$str,以$token为标志切割返回切割后的字符串

49.explode(): 使用一个字符串为标志分割另一个字符串

调用: array explode(str $sep,str $str[,int $limit])

输入: $sep为分割符,$str目标字符串,$limit返回数组最多包含元素数 
输出: 字符串被分割后形成的数组

50.implode(): 同join,将数组值用预订字符连接成字符串

调用: string implode ( string $glue , array $pieces )

$glue默认, 用”则直接相连

51.substr(): 截取字符串

调用: string substr ( string $string , int $start [, int $length ] )

字符串查找替换:

52.str_replace(): 字符串替换操作,区分大小写

Call mix str_replace(mix $search,mix $replace, mix $subject[,int &$num])

Eingabe:
$search String, Zeichenfolge ersetzt durch $replace, $subject durchsuchte Zeichenfolge, &$num
Ausgabe: Ersetztes Ergebnis zurückgeben

53.str_ireplace() String-Ersetzungsvorgang, Groß- und Kleinschreibung wird nicht beachtet

Aufruf: mix str_ireplace ( mix $search , mix $replace , mix $subject [, int &$count ] )

Eingabe:
$search string, $replace string, $subject durchsuchte Zeichenfolge, &$num
Ausgabe: Ersetztes Ergebnis zurückgeben

54.substr_count(): Zählt, wie oft eine Zeichenfolge vorkommt erscheint in einer anderen Zeichenfolge

Aufruf: int substr_count ( string $haystack , string $needle[, int $offset = 0 [, int $length ]] )

55.substr_replace(): Ersetzen Sie eine Zeichenfolge in einer Zeichenfolge durch eine andere Zeichenfolge

Aufruf: gemischt substr_replace ( gemischt $string, string $replacement,int $start [, int $length ] )

56.similar_text(): Gibt zwei Zeichenfolgen mit der gleichen Anzahl von Zeichen zurück

Aufruf: int Similar_text( str $str1,str $str2)
Eingabe:
Zwei verglichene Zeichenfolgen

Ausgabe:
Ganzzahl, gleiche Anzahl von Zeichen

57.strrchr(): Gibt das letzte Vorkommen einer Zeichenfolge in einer anderen Zeichenfolge vom Anfang bis zum Ende zurück. Zeichenfolge

Aufruf: string strrchr (string $haystack, Mixed $ Nadel)

58.strstr(): Gibt eine Zeichenfolge zurück. Die Zeichenfolge vom Anfang bis zum Ende einer anderen Zeichenfolge

heißt: string strstr ( string $str, string $needle, bool $before_needle )
 
59.strchr (): Alias ​​​​von strstr(), gibt einen String zurück, beginnend mit dem ersten Vorkommen eines Strings in einem anderen String das Ende der Zeichenfolge

Aufruf: string strstr ( string $haystack , Mixed $needle [, bool $before_needle = false ] )
 
60.stristr(): Gibt einen String vom Anfang bis zum Ende eines anderen Strings zurück, ohne Berücksichtigung der Groß-/Kleinschreibung

Aufruf: string stristr ( string $haystack , Mixed $needle [, bool $before_needle = false ] )

61.strtr(): String konvertieren Bestimmte Zeichen in

heißen: string strtr ( string $str , string $from , string $ to )

62.strpos( ): Finden Sie die Position, an der ein bestimmtes Zeichen zum ersten Mal in der Zeichenfolge erscheint

Aufruf: int strpos ( string $haystack , Mixed $needle [, int $offset = 0 ] )

63.stripos(): Findet das erste Vorkommen eines Zeichens in einer Zeichenfolge, ohne Berücksichtigung der Groß-/Kleinschreibung
Aufruf: int stripos ( string $haystack , string $needle [, int $offset ] )

64.strrpos(): Finde die letzte Position eines Zeichens in einem String

Aufruf: int strrpos ( string $ haystack, string $needle [, int $offset = 0 ] )

65.strripos(): Finde die letzte Position eines Zeichens in einer Zeichenfolge, ohne Berücksichtigung der Groß-/Kleinschreibung

Aufruf: int strripos ( string $haystack , string $needle [, int $offset ] )

66.strspn(): return string Die erste Substring-Längenanpassungsmaske in
heißt: int strspn ( string $str1 , string $str2 [, int $start [, int $length ]] )

67 .strcspn(): Gibt die Länge des Strings zurück, der nicht mit der Maske übereinstimmt

Aufruf: int strcspn ( string $str1 , string $str2 [, int $start [, int $ length ]] )

Eingabe:
$str1 wird abgefragt, $str2 Abfragezeichenfolge, $start ist das zu startende Zeichen Bei der Abfrage ist $length die Abfragelänge.
Ausgabe:
Gibt die Anzahl der Zeichen vom Anfang bis

String-Statistik:

68.str_word_count(): Zählt die im String Count enthaltenen Wörter

Aufruf: mix str_word_count(str $str,[])

Eingabe: Zielzeichenfolge Ausgabe: Menge der Statistikabteilung

69.strlen(): 统计字符串长度 
函数原型: int strlen(str $str)

输入: 目标字符串 
输出:整型长度

70.count_chars(): 统计字符串中所有字母出现次数(0..255) 
调用: mixed count_chars ( string $string [, int $mode ] )

字符串编码:

71.md5(): 字符串md5编码

<span style="font-size: 14px;">$str = "Hello";<br></span>

echo md5($str);

数组函数

数组创建:

72.array(): 生成一个数组

<span style="font-size: 14px;">    $a=array("Dog","Cat","Horse");<br>    print_r($a);<br></span>
  • 1

  • 2

  • 1

  • 2

数组值或,键=>值一个数组型变量

73.array_combine(): 生成一个数组,用一个数组的值 
作为键名,另一个数组值作为值

<span style="font-size: 14px;">    $a1=array("a","b","c","d");    $a2=array("Cat","Dog","Horse","Cow");<br>    print_r(array_combine($a1,$a2));<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

输入参数: $a1为提供键,$a2提供值 
输出: 合成后的数组

74.range(): 创建并返回一个包含指定范围的元素的数组。

<span style="font-size: 14px;">    $number = range(0,50,10);<br>    print_r ($number);<br></span>
  • 1

  • 2

  • 1

  • 2

输入: 0是最小值,50是最大值,10是步长 
输出: 合成后的数组

75.compact(): 创建一个由参数所带变量组成的数组

<span style="font-size: 14px;">    $firstname = "Peter";    $lastname = "Griffin";    $age = "38";    $result = compact("firstname", "lastname",  "age");<br>    print_r($result);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 1

  • 2

  • 3

  • 4

  • 5

变量或数组

返回由变量名为键,变量值为值的数组,变量也可以为多维数组.会递归处理 
76.array_fill(): 用给定的填充(值生成)数组

<span style="font-size: 14px;">    $a=array_fill(2,3,"Dog");<br>    print_r($a);<br></span>
  • 1

  • 2

  • 1

  • 2

2是键,3是填充的数量,’Dog’为填充内容返回完成的数组

数组合并和拆分:    

77.array_chunk(): 把一个数组分割为新的数组块

<span style="font-size: 14px;">    $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");<br>print_r(array_chunk($a,2));<br></span>
  • 1

  • 2

  • 1

  • 2

一个数组分割后的多维数组,规定每个新数组包含2个元素

78.array_merge(): 把两个或多个数组合并为一个数组。

<span style="font-size: 14px;">    $a1=array("a"=>"Horse","b"=>"Dog");    $a2=array("c"=>"Cow","b"=>"Cat");<br>    print_r(array_merge($a1,$a2));<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

输入: 两个数组 
输出: 返回完成后的数组

79.array_slice(): 在数组中根据条件取出一段值,并返回。

<span style="font-size: 14px;">    $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");<br>print_r(array_slice($a,1,2));<br></span>
  • 1

  • 2

  • 1

  • 2

输入: 一个数组 
输出: 1为从’Cat’开始,2为返回两个元素

数组比较:

80.array_diff(): 返回两个数组的差集数组

<span style="font-size: 14px;">    $a1=array(0=>"Cat",1=>"Dog",2=>"Horse");$a2=array(3=>"Horse",4=>"Dog",5=>"Fish");<br>    print_r(array_diff($a1,$a2)); //返回'Cat'<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

输入: 两个或多个数组 
输出: $a1与$a2的不同之处

81.array_intersect(): 返回两个或多个数组的交集数组 
输出: 
返回’Dog’和’Horse’, $a1与$a2的相同之处

数组查找替换:     

82.array_search(): 在数组中查找一个值,返回一个键,没有返回返回假

<span style="font-size: 14px;">    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");    echo array_search("Dog",$a);<br></span>
  • 1

  • 2

  • 1

  • 2

输入: 一个数组 
输出: 成功返回键名,失败返回false

83.array_splice(): 把数组中一部分删除用其他值替代

<span style="font-size: 14px;">    $a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");    $a2=array(0=>"Tiger",1=>"Lion");<br>    array_splice($a1,0,2,$a2);<br>    print_r($a1);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 1

  • 2

  • 3

  • 4

输入: 一个或多个数组 
输出: $a1被移除的部分由$a2补全

84.array_sum(): 返回数组中所有值的总和

<span style="font-size: 14px;">    $a=array(0=>"5",1=>"15",2=>"25");    echo array_sum($a);<br></span>
  • 1

  • 2

  • 1

  • 2

输入: 一个数组 
输出: 返回和

85.in_array(): 在数组中搜索给定的值,区分大小写

<span style="font-size: 14px;">    $people = array("Peter", "Joe", "Glenn", "Cleveland");    if (in_array("Glenn",$people) {    echo "Match found";<br>    }    else{    echo "Match not found";<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

输入: 需要搜索的值|数组 
输出: true/false

86.array_key_exists(): 判断某个数组中是否存在指定的 key

输入: 需要搜索的键名|数组

数组引用操作:

87.key(): 返回数组内部指针当前指向元素的键名 
    
88.current(): 返回数组中的当前元素(单元). 
    
89.next(): 把指向当前元素的指针移动到下一个元素的位置,并返回当前元素的值 
    
90.prev(): 把指向当前元素的指针移动到上一个元素的位置,并返回当前元素的值 
    
91.end(): 将数组内部指针指向最后一个元素,并返回该元素的值(如果成功) 
    
92.reset(): 把数组的内部指针指向第一个元素,并返回这个元素的值 
    
93.list(): 用数组中的元素为一组变量赋值

<span style="font-size: 14px;">    $my_array=array("Dog","Cat","Horse");    list($a, $b, $c) = $my_array;<br></span>
  • 1

  • 2

  • 1

  • 2

输入: $a, $b, $c为需要赋值的变量 
输出: 变量分别匹配数组中的值

94.array_shift(): 删除数组中的第一个元素,并返回被删除元素的值

<span style="font-size: 14px;">    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");    echo array_shift($a);<br>    print_r ($a);<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

95.array_unshift(): 在数组开头插入一个或多个元素

<span style="font-size: 14px;">    $a=array("a"=>"Cat","b"=>"Dog");<br>    array_unshift($a,"Horse");<br>    print_r($a);<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

96.array_push(): 向数组最后压入一个或多个元素

<span style="font-size: 14px;">$a=array("Dog","Cat");<br>array_push($a,"Horse","Bird");<br>print_r($a);<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

输入: 目标数组|需要压入的值 
返回值: 返回新的数组

97.array_pop(): 取得(删除)数组中的最后一个元素

<span style="font-size: 14px;">    $a=array("Dog","Cat","Horse");<br>    array_pop($a);<br>    print_r($a);<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

输入: $a为目标数组 
输出: 返回数组剩余元素

数组键值操作:     

98.shuffle(): 将数组打乱,保留键名

<span style="font-size: 14px;">    $my_array = array("a" => "Dog", "b" => "Cat");<br>    shuffle($my_array);<br>    print_r($my_array);<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

输入: 一个或多个数组 
输出: 顺序打乱后的数组

99.count(): 计算数组中的单元数目或对象中的属性个数

<span style="font-size: 14px;">    $people = array("Peter", "Joe", "Glenn",    "Cleveland");    $result = count($people);    echo $result;<br></span>
  • 1

  • 2

  • 3

  • 4

  • 1

  • 2

  • 3

  • 4

输入: 数组 
输出: 输出元素个数

100.array_flip(): 返回一个键值反转后的数组

<span style="font-size: 14px;">    $a=array(0=>"Dog",1=>"Cat",2=>"Horse");<br>print_r(array_flip($a));<br></span>
  • 1

  • 2

  • 1

  • 2

输出: 返回完成后的数组 
101.array_keys(): 返回数组所有的键,组成一个数组

<span style="font-size: 14px;">    $a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");<br>    print_r(array_keys($a));<br></span>
  • 1

  • 2

  • 1

  • 2

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

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

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

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

104.array_count_values(): 统计数组中所有的值出现的次数

<span style="font-size: 14px;">    $a=array("Cat","Dog","Horse","Dog");<br>    print_r(array_count_values($a));<br></span>
  • 1

  • 2

  • 1

  • 2

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

105.array_rand(): 从数组中随机抽取一个或多个元素,注意是键名!!!

<span style="font-size: 14px;">    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");<br>    print_r(array_rand($a,1));<br></span>
  • 1

  • 2

  • 1

  • 2

$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(): 删除重复值,返回剩余数组

<span style="font-size: 14px;">    $a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");<br>    print_r(array_unique($a));<br></span>
  • 1

  • 2

  • 1

  • 2

输入: 数组 
输入: 返回无重复值数组,键名不变

数组排序:   

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

<span style="font-size: 14px;">    $my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");<br>    sort($my_array);<br>    print_r($my_array);<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

输出: true/false 
  
109.rsort(): 对数组逆向排序,不保留键名   
110.asort(): 对数组排序,保持索引关系    
111.arsort(): 对数组逆向排序,保持索引关  
112.ksort(): 系按键名对数组排序  
113.krsort(): 将数组按照键逆向排序 
114.natsort(): 用自然顺序算法对数组中的元素排序     
115.natcasesort(): 自然排序,不区分大小写    
  

文件系统函数

116.fopen(): 打开文件或者 URL

<span style="font-size: 14px;">    $handle = fopen("ftp://user:password@example.com/somefile.txt", "w");<br></span>
  • 1

  • 1

调用: resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )

返回值: 如果打开失败,本函数返回 FALSE

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

<span style="font-size: 14px;">    $handle = fopen('somefile.txt', 'r');<br>    fclose($handle);<br>    bool fclose(resource handle)<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

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

文件属性

118.file_exists(): 检查文件或目录是否存在

<span style="font-size: 14px;">    $filename = '/path/to/foo.txt';    if (file_exists($filename)) {    echo "exists";<br>    } else {    echo "does not exist";<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

调用: bool file_exists ( string filename ) 
输入: 指定的文件或目录 
输出: 存在则返回 TRUE,否则返回 FALSE

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

<span style="font-size: 14px;">    $filename = 'somefile.txt';echo $filename . ': ' . filesize($filename) .'bytes';<br></span>
  • 1

  • 2

  • 1

  • 2

调用: int filesize ( string $filename )

输出: 返回文件大小的字节数,如果出错返回 FALSE 并生成一条 E_WARNING 级的错误

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

<span style="font-size: 14px;">    $filename = 'test.txt';    if (is_readable($filename)) {    echo '可读';<br>    } else {    echo '不可读';<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

调用: bool is_readable ( string $filename ) 
输出: 如果由 filename指定的文件或目录存在并且可读则返回 TRUE

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

<span style="font-size: 14px;">    $filename = 'test.txt';    if (is_writable($filename)) {    echo '可写';<br>    } else {    echo '不可写';<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

调用: bool is_writable ( string $filename ) 
filename 参数 可以是一个允许进行是否可写检查的目录名

输出: 
如果文件存在并且可写则返回 TRUE。

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

<span style="font-size: 14px;">    $file = 'setup.exe';    if (is_executable($file)) {    echo '可执行';<br>    } else {    echo '不可执行';<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

调用: bool is_executable ( string $filename ) 
输出: 如果文件存在且可执行则返回 TRUE

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

<span style="font-size: 14px;">    $filename = 'somefile.txt';echo filectime($filename);<br></span>
  • 1

  • 2

  • 1

  • 2

调用: int filectime ( string $filename ) 
输出: 时间以 Unix 时间戳的方式返回,如果出错则返回FALSE

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

<span style="font-size: 14px;">    $filename = 'somefile.txt';echo filemtime($filename);<br>    int filemtime ( string $filename )<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

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

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

<span style="font-size: 14px;">    $filename = 'somefile.txt';echo fileatime($filename);<br></span>
  • 1

  • 2

  • 1

  • 2

调用: int fileatime (string $filename)

输出: 返回文件上次被访问的时间, 如果出错则返回FALSE. 时间以Unix时间戳的方式返回.

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

<span style="font-size: 14px;">    $filename = 'somefile.txt';<br>var_dump(fileatime($filename));<br></span>
  • 1

  • 2

  • 1

  • 2

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

文件操作

127.fwrite(): 写入文件

<span style="font-size: 14px;">    $filename = 'test.txt';    $somecontent = "添加这些文字到文件\n";    $handle = fopen($filename, 'a');<br>    fwrite($handle, $somecontent);<br>    fclose($handle);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 1

  • 2

  • 3

  • 4

  • 5

调用: int fwrite ( resource handle, string string [, int length] )

输出: 
把 string 的内容写入 文件指针 handle 处。如果指定了 length,当写入了length个字节或者写完了string以后,写入就会停止, 视乎先碰到哪种情况

128.fputs(): 同上  
   
129.fread(): 读取文件

<span style="font-size: 14px;">    $filename = "/usr/local/something.txt";$handle = fopen($filename, "r");$contents = fread($handle, filesize($filename));<br>    fclose($handle);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 1

  • 2

  • 3

  • 4

调用: string fread ( int handle, int length ) 
从文件指针handle,读取最多 length 个字节

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

<span style="font-size: 14px;">    $file = @fopen("no_such_file", "r");    while (!feof($file)) {<br>    }<br>    fclose($file);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 1

  • 2

  • 3

  • 4

调用: bool feof ( resource handle ) 
输出: 如果文件指针到了 EOF 或者出错时则返回TRUE,否则返回一个错误(包括 socket 超时),其它情况则返回 FALSE

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

<span style="font-size: 14px;">    $handle = @fopen("/tmp/inputfile.txt", "r");    if ($handle) {    while (!feof($handle)) {    $buffer = fgets($handle, 4096);    echo $buffer;<br>    }<br>    fclose($handle);<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

调用: string fgets ( int handle [, int length] ) 
输出: 从handle指向的文件中读取一行并返回长度最多为length-1字节的字符串.碰到换行符(包括在返回值中)、EOF 或者已经读取了length -1字节后停止(看先碰到那一种情况). 如果没有指定 length,则默认为1K, 或者说 1024 字节.

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

<span style="font-size: 14px;">    $fp = fopen('somefile.txt', 'r');    if (!$fp) {    echo 'Could not open file somefile.txt';<br>    }    while (false !== ($char = fgetc($fp))) {    echo "$char\n";<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

输入: string fgetc ( resource $handle ) 
输出: 返回一个包含有一个字符的字符串,该字符从 handle指向的文件中得到. 碰到 EOF 则返回 FALSE.

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

<span style="font-size: 14px;">    $lines = file('http://www.example.com/');<br></span>
  • 1

  • 1

// 在数组中循环,显示 HTML 的源文件并加上行号。

<span style="font-size: 14px;">    foreach ($lines as $line_num => $line) {    echo "Line #<b>{$line_num}</b> : " .<br>    htmlspecialchars($line) . "<br />\n";<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 1

  • 2

  • 3

  • 4

// 另一个例子将 web 页面读入字符串。参见 file_get_contents()。

<span style="font-size: 14px;">    $html = implode('', file('http://www.example.com/'));<br></span>
  • 1

  • 1

调用: 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(): 将整个文件读入一个字符串

<span style="font-size: 14px;">    echo file_get_contents('http://www.baidu.com');<br></span>
  • 1

  • 1

调用: 
string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] ) 
  
136.file_put_contents():将一个字符串写入文件

<span style="font-size: 14px;">    file_put_contents('1.txt','aa');<br></span>
  • 1

  • 1

调用: int file_put_contents ( string $filename , string $data [, int $flags [, resource $context ]] )

输出: 该函数将返回写入到文件内数据的字节数

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

<span style="font-size: 14px;">    $fp=fopen('tx.txt','r');<br>    fseek($fp,10);    echo ftell($fp);<br>    fread($fp,4);    echo ftell($fp);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 1

  • 2

  • 3

  • 4

  • 5

调用: int ftell ( resource $handle ) 
输出: 返回由 handle 指定的文件指针的位置,也就是文件流中的偏移量

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

<span style="font-size: 14px;">    $fp=fopen('tx.txt','r');<br>    fseek($fp,10);    echo ftell($fp);<br>    fread($fp,4);    echo ftell($fp);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 1

  • 2

  • 3

  • 4

  • 5

调用: int fseek ( resource $handle , int $offset [, int $whence ] ) 
输出: 成功则返回 0;否则返回 -1

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

<span style="font-size: 14px;">    $fp=fopen('tx.txt','r');<br>    fseek($fp,3);    echo ftell($fp);<br>    fread($fp,4);<br>    rewind($fp);    echo ftell($fp);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

调用: bool rewind ( resource $handle ) 
返回值: 如果成功则返回 TRUE,失败则返回 FALSE

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

<span style="font-size: 14px;">    $fp=fopen('tx.txt','r');<br>    flock($fp, LOCK_SH);//共享锁<br>    //flock($fp, LOCK_EX);//独立锁,写文件时用它打开<br>    //flock($fp, LOCK_NB);//附加锁<br>    flock($fp, LOCK_UN);//释放锁<br>    fclose($fp);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

调用: bool flock ( int $handle , int $operation [, int &$wouldblock ] ) 
输出: 如果成功则返回 TRUE,失败则返回 FALSE

目录

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

<span style="font-size: 14px;">    path = "/home/httpd/html/index.php";    $file = basename($path);    $file = basename($path,".php");<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

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

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

<span style="font-size: 14px;">    $path = "/etc/passwd";    $file = dirname($path);<br></span>
  • 1

  • 2

  • 1

  • 2

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

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

<span style="font-size: 14px;">    echo '<pre class="brush:php;toolbar:false">';<br>    print_r(pathinfo("/www/htdocs/index.html"));    echo '
';
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

调用: mixed pathinfo ( string $path [, int $options ] ) 
返回一个关联数组包含有 path 的信息

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

<span style="font-size: 14px;">$fp=opendir('E:/xampp/htdocs/php/study/19');echo readdir($fp);<br>closedir($fp);<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

调用: resource opendir ( string $path [, resource $context ] ) 
返回值: 如果成功则返回目录句柄的 resource,失败则返回FALSE

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

<span style="font-size: 14px;">$fp=opendir('E:/xampp/htdocs/php/study/19');echo readdir($fp);<br>closedir($fp);<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

调用: string readdir ( resource $dir_handle ) 
返回值: 返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回

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

<span style="font-size: 14px;">    $fp=opendir('E:/xampp/htdocs/php/study/19');    echo readdir($fp);<br>    closedir($fp);<br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

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

<span style="font-size: 14px;">    $fp=opendir('E:/xampp/htdocs/php/study/19');    echo readdir($fp).'<br />';    echo readdir($fp).'<br />';    echo readdir($fp).'<br />';<br>    rewinddir($fp);    echo readdir($fp).'<br />';<br>    closedir($fp);<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

调用: void rewinddir ( resource $dir_handle ) 
输出: 将 dir_handle 指定的目录流重置到目录的开头 
148.mkdir(): 新建目录

<span style="font-size: 14px;">    mkdir('123');<br></span>
  • 1

  • 1

调用: bool mkdir ( string $pathname [, int $mode [, bool $recursive [, resource $context ]]] ) 
输出: 尝试新建一个由 pathname 指定的目录

149.rmdir(): 删除目录

<span style="font-size: 14px;">    rmdir('123');<br></span>
  • 1

  • 1

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

150.unlink(): 删除文件

<span style="font-size: 14px;">    unlink('123/1.txt');<br>    rmdir('123');<br></span>
  • 1

  • 2

  • 1

  • 2

调用: bool unlink ( string $filename ) 
输出: 删除 filename 。和 Unix C 的 unlink() 函数相似。如果成功则返回 TRUE,失败则返回 FALSE

151.copy(): 拷贝文件

<span style="font-size: 14px;">    copy('index.php','index.php.bak');<br></span>
  • 1

  • 1

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

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

<span style="font-size: 14px;">    rename('tx.txt','txt.txt');<br></span>
  • 1

  • 1

调用: bool rename ( string $oldname , string $newname [, resource $context ] ) 
输出: 如果成功则返回 TRUE,失败则返回 FALSE

文件的上传与下载

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

<span style="font-size: 14px;">    if(is_uploaded_file($_FILES['bus']['tmp_name'])){    if( move_uploaded_file($_FILES['bus']['tmp_name'],    $NewPath) ){    echo '上传成功<br /><img src="&#39;.$NewPath.&#39;">';<br>    }else{    exit('失败');<br>    }<br>    }else{    exit('不是上传文件');<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

调用: bool is_uploaded_file ( string $filename )  

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

<span style="font-size: 14px;">    if(is_uploaded_file($_FILES['bus']['tmp_name'])){    if( move_uploaded_file($_FILES['bus']['tmp_name'],    $NewPath) ){    echo '上传成功<br /><img src="&#39;.$NewPath.&#39;">';<br>    }else{    exit('失败');<br>    }<br>    }else{    exit('不是上传文件');<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

调用: bool move_uploaded_file ( string $filename , string

时间函数

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

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

<span style="font-size: 14px;">    mktime(0, 0, 0, 4, 25, 2012);<br></span>
  • 1

  • 1

调用: int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] ) 
  
156.date(): 格式化一个本地时间/日期

<span style="font-size: 14px;">date('Y年m月d日 H:i:s');<br></span>
  • 1

  • 1

调用: string date ( string $format [, int $timestamp ] )

输出: 2016年09月10日 20:45:54

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

<span style="font-size: 14px;">    if(checkdate(6,31,2012)){    echo '成立';<br>    }else{    echo '不成立';<br>    }<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 1

  • 2

  • 3

  • 4

  • 5

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

<span style="font-size: 14px;">    date_default_timezone_set('PRC');<br></span>
  • 1

  • 1

调用: bool date_default_timezone_set ( string $timezone_identifier)

返回值: 如果 timezone_identifier 参数无效则返回 FALSE,否则返回 TRUE。

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

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

<span style="font-size: 14px;">    $t=getdate();<br>    var_dump($t);<br></span>
  • 1

  • 2

  • 1

  • 2

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

<span style="font-size: 14px;">    echo strtotime("now");<br>    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");<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

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

<span style="font-size: 14px;">    $start=microtime(true);<br>    sleep(3);    $stop=microtime(true);    echo $stop-$start;<br></span>
  • 1

  • 2

  • 3

  • 4

  • 1

  • 2

  • 3

  • 4

其他常用:

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。

163.sprintf(): 函数把格式化的字符串写入一个变量中。

语法 
sprintf(format,arg1,arg2,arg++) 
参数 描述 
format 必需。转换格式。 
arg1 必需。规定插到 format 字符串中第一个 % 符号处的参数。 
arg2 可选。规定插到 format 字符串中第二个 % 符号处的参数。 
arg++ 可选。规定插到 format 字符串中第三、四等等 % 符号处的参数。 
说明 
参数 format 是转换的格式,以百分比符号 (“%”) 开始到转换字符结束。下面的可能的 format 值:

%% - 返回百分比符号 
%b - 二进制数 
%c - 依照 ASCII 值的字符 
%d - 带符号十进制数 
%e - 可续计数法(比如 1.5e+3) 
%u - 无符号十进制数 
%f - 浮点数(local settings aware) 
%F - 浮点数(not local settings aware) 
%o - 八进制数 
%s - 字符串 
%x - 十六进制数(小写字母) 
%X - 十六进制数(大写字母) 
arg1, arg2, ++ 等参数将插入到主字符串中的百分号 (%) 符号处。该函数是逐步执行的。在第一个 % 符号中,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。

提示和注释

注释:如果 % 符号多于 arg 参数,则您必须使用占位符。占位符插到 % 符号后面,由数字和 “$” 组成。请参见例子 3。

提示:相关函数:fprintf()、printf()、vfprintf()、vprintf() 以及 vsprintf()。

例子

例子 1

<span style="font-size: 14px;"><?php$str = "Hello";$number = 123;$txt = sprintf("%s world. Day number %u",$str,$number);echo $txt;?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

输出:

Hello world. Day number 123

164.PDO类的相关函数 
prepare() 
execute() 
fetch()

<span style="font-size: 14px;"><?php$driver = &#39;mysql&#39;;$database = "dbname=CODINGGROUND";$dsn = "$driver:host=localhost;unix_socket=/home/cg/mysql/mysql.sock;$database";$username = &#39;root&#39;;$password = &#39;root&#39;;try {   $conn = new PDO($dsn, $username, $password);   echo "<h2>Database CODINGGROUND Connected<h2>";<br>}catch(PDOException $e){   echo "<h1>" . $e->getMessage() . "</h1>";<br>}$sql = 'SELECT  * FROM users';$stmt = $conn->prepare($sql);$stmt->execute();echo "<table style=&#39;width:100%&#39;>";while($row = $stmt->fetch(PDO::FETCH_ASSOC)){  echo "<tr>";  foreach($row as $value)<br>  {    echo sprintf("<td>%s</td>", $value);<br>  }  echo "</tr>";<br>}echo "</table>";?><br></span>
  • 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

  • 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

165.isset(): 检测变量是否设置。 
原型格式: bool isset ( mixed var [, mixed var [, ...]] )

返回值: 
若变量不存在则返回 FALSE 
若变量存在且其值为NULL,也返回 FALSE 
若变量存在且值不为NULL,则返回 TURE 
同时检查多个变量时,每个单项都符合上一条要求时才返回 TRUE,否则结果为 FALSE 
如果已经使用 unset() 释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。同时要注意的是一个 NULL 字节(”“)并不等同于 PHP 的 NULL 常数。

<span style="font-size: 14px;">$userInfo=’abc’;if(isset($userInfo['account'])) {$account=$userInfo['account'];<br>} else {$account=$userInfo;<br>}<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

166.unset(): 销毁指定的变量。 
函数原型: unset(var1,var2,...)

参数 描述 
var1 要销毁的变量1 
var2 要销毁的变量2

<span style="font-size: 14px;"><?php<br/>    $foo = &#39;php unset()&#39;;    unset ($foo);    echo $foo;?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 1

  • 2

  • 3

  • 4

  • 5

167.preg_replace_callback: 执行一个正则表达式搜索并且使用一个回调进行替换.

原型: 
mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] ) 
这个函数的行为除了 可以指定一个 callback 替代 replacement 进行替换 字符串的计算,其他方面等同于 preg_replace()。

pattern: 要搜索的模式,可以使字符串或一个字符串数组。

callback: 一个回调函数,在每次需要替换时调用,调用时函数得到的参数是从subject 中匹配到的结果。回调函数返回真正参与替换的字符串。这是该回调函数的签名: 
string handler ( array $matches ) 
你可能经常会需要callback函数而仅用于preg_replace_callback()一个地方的调用。在这种情况下,你可以 使用匿名函数来定义一个匿名函数作为preg_replace_callback()调用时的回调。 这样做你可以保留所有 调用信息在同一个位置并且不会因为一个不在任何其他地方使用的回调函数名称而污染函数名称空间。

subject: 要搜索替换的目标字符串或字符串数组。

limit: 对于每个模式用于每个 subject 字符串的最大可替换次数。 默认是-1(无限制)。

count: 如果指定,这个变量将被填充为替换执行的次数。

<span style="font-size: 14px;"><?php/* 一个unix样式的命令行过滤器,用于将段落开始部分的大写字母转换为小写。 */$fp = fopen("php://stdin", "r") or die("can&#39;t read stdin");while (!feof($fp)) {    $line = fgets($fp);    $line = preg_replace_callback(        &#39;|<p>\s*\w|',        function ($matches) {<br>            return strtolower($matches[0]);<br>        },        $line<br>    );    echo $line;<br>}<br>fclose($fp);?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

返回值: 
如果subject是一个数组, preg_replace_callback()返回一个数组,其他情况返回字符串。 错误发生时返回 NULL。

如果查找到了匹配,返回替换后的目标字符串(或字符串数组), 其他情况subject 将会无变化返回。

168.json_encode(): 对变量进行 JSON 编码

函数原型: json_encode(value,option) 
参数 描述 
value 必填。待编码的 value ,除了resource 类型之外,可以为任何数据类型。该函数只能接受 UTF-8 编码的数据 
options 可选。 
JSON_HEX_QUOT 把双引号转为\u0022(php 5.3) 
JSON_HEX_TAG 把< > 转为 \u003C 和 \u003E(php 5.3) 
JSON_HEX_AMP 把 & 转为 \u0026(php 5.3) 
JSON_HEX_APOS 把单引号转为 \u0027.(php 5.3) 
JSON_NUMERIC_CHECK 把数字字符串当作数字编码(php 5.3) 
JSON_PRETTY_PRINT 使用空格格式化数据(php 5.4) 
JSON_UNESCAPED_SLASHES 不忽略 /(php 5.4) 
JSON_FORCE_OBJECT 使用非关联数组时输出一个对象而不是一个数组(php 5.3) 
JSON_UNESCAPED_UNICODE 逐字编译多字节字符(php 5.4)

<span style="font-size: 14px;"><?php$arr = array (&#39;a&#39;=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);echo json_encode($arr);?> <br></span>
  • 1

  • 2

  • 3

  • 4

  • 1

  • 2

  • 3

  • 4

以上例程会输出:

<span style="font-size: 14px;">{"a":1,"b":2,"c":3,"d":4,"e":5}<br></span>

169.iconv(): 用于按规定的字符编码转换字符串。mb_convert_encoding() 函数也可以转换编码。

如果发现中文输出乱码的时候,很可能就需要使用此函数做处理。

函数原型: iconv(in_charset ,out_charset ,str ) 
参数 描述 
in_charset 输入的字符集。 
out_charset 输出的字符集。如果你在 out_charset 后添加了字符串 //TRANSLIT,将启用转写(transliteration)功能。这个意思是,当一个字符不能被目标字符集所表示时,它可以通过一个或多个形似的字符来近似表达。 如果你添加了字符串 //IGNORE,不能以目标字符集表达的字符将被默默丢弃。 否则,str 从第一个无效字符开始截断并导致一个 E_NOTICE。 
str 要转换的字符串。

<span style="font-size: 14px;"><?php$text = "This is the Euro symbol &#39;€&#39;.";echo &#39;Original : &#39;, $text, PHP_EOL;echo &#39;TRANSLIT : &#39;, iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL;echo &#39;IGNORE   : &#39;, iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL;echo &#39;Plain    : &#39;, iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

输出:

<span style="font-size: 14px;">Original : This is the Euro symbol '€'.<br>TRANSLIT : This is the Euro symbol 'EUR'.<br>IGNORE   : This is the Euro symbol ''.<br>Plain    :<br>Notice: iconv(): Detected an illegal character in input string in .\iconv-example.php on line 7This is the Euro symbol '<br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

170.exec(): 不输出结果,返回最后一行执行结果,所有结果可以保存到一个返回的数组里面。

函数原型: 
exec(command,output,return_var) 
参数 描述 
command 必需。要执行的命令 
output 可选。一个包含每行命令执行结果的数组 
return_var 可选。获得命令执行的状态码

注意:dir命令适应windows系统,Linux系统请使用ls命令。

<span style="font-size: 14px;"><?php$l = exec("dir",$out,$stat);<br/>print_r($l);<br/>print_r($out);<br/>print_r($stat);?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

171.system(): 调用命令,输出并返回最后一个执行结果。 
函数原型: system(command,return_var) 
参数 描述 
command 必需。要执行的命令 
return_var 可选。得到命令执行后的状态码

<span style="font-size: 14px;"><?php$l = system("dir",$arr);<br/>print_r($arr);<br/>print_r($l);?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 1

  • 2

  • 3

  • 4

  • 5

172.serialize():作用是序列化对象、数组成一个字符串。 
比如存储数组到memcache就会被序列化。使用 unserialize() 可以还原数组。

语法 
serialize(param) 
参数 描述 
param 必需。对象或数组。

<span style="font-size: 14px;"><?php$arr = array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;serialize&#39;,&#39;怎么用&#39;);echo serialize($arr),&#39;<br/>';// 还原序列化的数据print_r(unserialize(serialize($arr)));?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

输出:

<span style="font-size: 14px;">a:5:{i:0;s:1:"a";i:1;s:1:"b";i:2;s:1:"c";i:3;s:9:"serialize";i:4;s:9:"怎么用";}Array ( [0] => a [1] => b [2] => c [3] => serialize [4] => 怎么用 )<br></span>
  • 1

  • 2

  • 1

  • 2

173.php_check_syntax 
这个函数可以用来检查特定文件中的PHP语法是否正确。 
使用方法:

<span style="font-size: 14px;"><?php$error_message = "";$filename = "./php_script.php";if(!php_check_syntax($filename, &$error_message)) {   echo "Errors were found in the file $filename: $error_message";<br/>} else {   echo "The file $filename contained no syntax errors";<br/>}?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

174.highlight_string 
当你想要把PHP代码显示到页面上时,highlight_string()函数就会非常有用,它可以用内置定义的语法高亮颜色把你提供的PHP代码高亮显示。这个函数有两个参数,第一个参数是要被突出显示的字符串。第二个参数如果设置成TRUE,就会把高亮后的代码返回。 
使用方法:

<span style="font-size: 14px;"><?phphighlight_string(&#39; <?php phpinfo(); ?>');?><br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

175.show_source 
这个函数的运行和highlight_file()相似,它可以显示PHP语法高亮后的文件,并且是根据HTML标签进行语法高亮的。 
使用方法:

<span style="font-size: 14px;"><?phpshow_source("php_script.php");?><br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

176.php_strip_whitespace 
这个函数跟上面的show_source()函数相似,但它会删除文件里的注释和空格符。 
使用方法:

<span style="font-size: 14px;"><?phpecho php_strip_whitespace("php_script.php");?><br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

177.halt_compiler 
它可以中止编译器的执行,这对于在PHP脚本中嵌入数据是很有帮助的,就像安装文件一样。 
使用方法:

<span style="font-size: 14px;"><?php$fp = fopen(__FILE__, &#39;r&#39;);<br/>fseek($fp, __COMPILER_HALT_OFFSET__);<br/>var_dump(stream_get_contents($fp));// the end of the script execution__halt_compiler(); <br/>?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

178.highlight_file 
这是一个非常有用的PHP函数,它能返回指定的PHP文件,并按照语法高亮突出显示文件内容。 
使用方法:

<span style="font-size: 14px;"><?phphighlight_file("php_script.php");?><br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

179.ignore_user_abort 
利用这个函数,用户可以拒绝浏览器端终止执行脚本的请求。正常情况下客户端的退出会导致服务器端脚本的停止运行。 
使用方法:

<span style="font-size: 14px;"><?phpignore_user_abort();?><br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

180.str_word_count 
这个函数可以用来统计字符串中单词的数量。 
使用方法:

<span style="font-size: 14px;"><?phpecho str_word_count("Hello How Are You!");?><br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

181.get_defined_vars 
这个函数在代码调试时十分重要,它会返回一个包括所有已定义的变量的多维数组。 
使用方法:

<span style="font-size: 14px;"><?phpprint_r(get_defined_vars());?><br></span>
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

182.get_browser 
这个函数检查并读取browscap.ini文件,返回浏览器兼容信息。 
使用方法:

<span style="font-size: 14px;"><?phpecho $_SERVER[&#39;HTTP_USER_AGENT&#39;];$browser = get_browser();<br/>print_r($browser);?><br></span>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 1

  • 2

  • 3

  • 4

  • 5

正则表达式-元字符

元字符及其匹配范围

\d 匹配任意一个十进制数字,等价于: [0-9] 
\D 匹配除十进制数字以外的任意数字,等价于: [^0-9] 
\s:匹配空白字符,等价于: [\n\f\r\t\v] 
\S: 匹配除空白字符以外的任意一个字符, 等价于[^\n\f\r\t\v]

\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次 
 

Andere

Entspricht jedem Zeichen außer Newline (n) [entspricht auch fr unter Windows]
|. Zwei oder mehr Zweigauswahlen [Niedrigste Priorität]
^ Entspricht der Startposition des Eingabezeichens
$ Entspricht der Endposition des Eingabezeichens
b Entspricht der Wortgrenze
B Entspricht der Nicht-Wortgrenze
() 1) Mustereinheit, Viele kleine Atome bilden ein großes Atom. 2) Sie können die Priorität ändern

Verwandte Empfehlungen:

Grundstruktur der PHP-Funktion

Wie wäre es? PHP-Funktion Detaillierte Erläuterung von Beispielen für die Verwendung einer variablen Anzahl von Parametern

Wozu dient das kaufmännische Und vor der PHP-Funktion

Beispiele für Beliebige Anzahl von Parametern und Suchdateien für PHP-Funktionen. Detaillierte Erklärung

Detaillierte Erklärung von 7 praktischen Anwendungsbeispielen für PHP-Funktionen

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn