0. PHP&HTML混写

  • 99乘法表的显示
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <table>
  11. <?php
  12. for ($i=1; $i <=9; $i++) { ?>
  13. <tr>
  14. <?php
  15. for ($j=1; $j <= $i ; $j++) {
  16. $ret = $i * $j;
  17. ?>
  18. <td style="border: 1px solid blue">
  19. <?php echo "$j * $i = $ret" ?>
  20. </td>
  21. <?php } ?>
  22. <?php } ?>
  23. </table>
  24. </body>
  25. </html>

— 数组展示

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <?php
  11. $stus = [
  12. ["name"=>"Tom", "age"=>24, "gender"=>"male", "address"=>"Beijing"],
  13. ["name"=>"Jack", "age"=>26, "gender"=>"male", "address"=>"Beijing"],
  14. ["name"=>"Rose", "age"=>42, "gender"=>"male", "address"=>"Beijing"],
  15. ["name"=>"Tony", "age"=>21, "gender"=>"male", "address"=>"Beijing"],
  16. ];
  17. ?>
  18. <table>
  19. <tr>
  20. <th>姓名</th>
  21. <th>年龄</th>
  22. <th>性别</th>
  23. <th>地址</th>
  24. </tr>
  25. <?php foreach($stus as $stu){ ?>
  26. <tr>
  27. <?php
  28. foreach($stu as $val){ ?>
  29. <td>
  30. <?php echo $val; ?>
  31. </td>
  32. <?php } ?>
  33. </tr>
  34. <?php } ?>
  35. </table>
  36. </body>
  37. </html>

1. 字符串操作

一个字符串 string 就是由一系列的字符组成,其中每个字符等同于一个字节。如果是汉字,根据编码计算所占字节数.

2. 定符串定义

  • 单引号形式
    ``` // 不转义,所见即所得 $s1 = ‘hello’;

$s2 = ‘hello $s1 \n’;

  1. - 双引号形式
  2. ```php
  3. // 转义变量或转义特殊字符
  4. $s1 = "hello";
  5. $s2 = "hello $s1 \n";

3. 获取字符串长度函数

  • strlen() -返回给定的字符串 string 的长度。

    1. strlen(string $string): int
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. $s1 = "helloworld";
    4. echo strlen($s1);
    5. echo "<br>";
    6. $s2 = "你好";
    7. echo strlen($s2);
    8. echo "<br>";
    9. ?>

4. 查找相关

  • strpos() — 查找字符串首次出现的位置

    1. strpos(string $haystack, mixed $needle, int $offset = 0): int
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. // 可查找单个字符
    4. $s1 = "helloworld";
    5. echo strpos($s1,"o");
    6. echo "<br>";
    7. // 可查多个字符
    8. $s2 = "哈喽你好你好";
    9. echo strpos($s2, "你");
    10. echo "<br>";
    11. echo strpos($s2, "你好");
    12. echo "<br>";
    13. ?>
  • strrpos() — 计算指定字符串在目标字符串中最后一次出现的位置, 反向查找;

    1. strrpos(string $haystack, string $needle, int $offset = 0): int
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. // 可查找单个字符
    4. $s1 = "helloworld";
    5. echo strrpos($s1,"o");
    6. echo "<br>";
    7. // 可查多个字符
    8. $s2 = "哈喽你好你好";
    9. echo strrpos($s2, "你");
    10. echo "<br>";
    11. echo strrpos($s2, "你好");
    12. echo "<br>";
    13. ?>
  • stripos() — 查找字符串首次出现的位置(不区分大小写)

    1. stripos(string $haystack, string $needle, int $offset = 0): int
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. // 可查找单个字符
    4. $s1 = "heLloworld";
    5. echo stripos($s1,"l");
    6. echo "<br>";
    7. ?>
  • strripos() — 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)

    1. strripos(string $haystack, string $needle, int $offset = 0): int
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. // 可查找单个字符
    4. $s1 = "heLloworld";
    5. echo strripos($s1,"l");
    6. echo "<br>";
    7. ?>

5. 统计相关

  • str_word_count — 返回字符串中单词的使用情况
    format参数:
    指定函数的返回值。当前支持的值如下:

    1. str_word_count(string $string, int $format = 0, ?string $characters = null): array|int
    • 0 - 返回单词数量(默认)
    • 1 - 返回一个包含 string 中全部单词的数组
    • 2 - 返回关联数组。数组的键是单词在 string 中出现的数值位置索引下标,数组的值是这个单词

注意:请注意,不支持多字节编码的字符串(不支持汉字统计)。

  1. <?php
  2. header("content-type:text/html;charset=utf8");
  3. $str = "Hello fri3nd, you're
  4. looking good today!";
  5. echo str_word_count($str);
  6. echo "<br>";
  7. print_r(str_word_count($str, 0));
  8. echo "<br>";
  9. print_r(str_word_count($str, 1));
  10. echo "<br>";
  11. print_r(str_word_count($str, 2));
  12. ?>
  • substr_count — 计算字串出现的次数

    1. substr_count(
    2. string $haystack,
    3. string $needle,
    4. int $offset = 0,
    5. ?int $length = null
    6. ): int

    ```php <?php header(“content-type:text/html;charset=utf8”); $text = ‘This is a test’;

    // 查找全部字符串内容 echo substr_count($text, ‘is’); // 2

    // 字符串被简化为 ‘s is a test’,因此输出 1 echo substr_count($text, ‘is’, 3);

    // 字符串被简化为 ‘s i’,所以输出 0 echo substr_count($text, ‘is’, 3, 3);

    // 因为 5+10 > 14,所以生成警告 // echo substr_count($text, ‘is’, 5, 10);

// 输出 1,因为该函数不计算重叠字符串 $text2 = ‘gcdgcdgcd’; echo substr_count($text2, ‘gcdgcd’); ?>

  1. <a name="a2cc5f9d"></a>
  2. #### 6. 比较相关
  3. - [strcmp](https://www.php.net/manual/zh/function.strcmp.php) — 二进制安全字符串比较<br />如果 `str1` 小于 `str2` 返回 < 0; 如果 `str1` 大于 `str2` 返回 > 0;如果两者相等,返回 0。(返回值为两串相减的差值)
  4. ```php
  5. strcmp(string $str1, string $str2): int
  1. <?php
  2. header("content-type:text/html;charset=utf8");
  3. echo strcmp("a", "b");
  4. echo "<br>";
  5. echo strcmp("a", "a");
  6. echo "<br>";
  7. echo strcmp("b", "a");
  8. echo "<br>";
  9. echo strcmp("abc", "z");
  10. echo "<br>";
  11. ?>
  • strcasecmp — 二进制安全比较字符串(不区分大小写)
    如果 str1 小于 str2 返回 < 0; 如果 str1 大于 str2 返回 > 0;如果两者相等,返回 0。

    1. strcasecmp(string $str1, string $str2): int
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. echo strcasecmp("a", "b");
    4. echo "<br>";
    5. echo strcasecmp("a", "a");
    6. echo "<br>";
    7. echo strcasecmp("b", "a");
    8. echo "<br>";
    9. echo strcasecmp("abc", "z");
    10. echo "<br>";
    11. echo strcasecmp("A", "a");
    12. echo "<br>";
    13. echo strcasecmp("ab", "Z");
    14. echo "<br>";
    15. ?>
  • strncmp — 二进制安全比较字符串开头的若干个字符
    该函数与 strcmp() 类似,不同之处在于你可以指定两个字符串比较时使用的长度(即最大比较长度)。
    注意该比较区分大小写。

    1. strncmp(string $str1, string $str2, int $len): int
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. echo strncmp("abcc", "abcd", 3);
    4. echo "<br>";
    5. ?>

7. 替换相关

  • str_replace — 子字符串替换

    1. str_replace(
    2. mixed $search,
    3. mixed $replace,
    4. mixed $subject,
    5. int &$count = ?
    6. ): mixed

    ```php <?php header(“content-type:text/html;charset=utf8”);

    // 替换不会改变原字符串,会生成一个新字符串 $old = “aa”; $new = “AA”; $str = “aaBBaaBBaaBB”; $newStr = str_replace($old, $new, $str); echo $str; echo “
    “; echo $newStr; echo “
    “;

    // 查找值可以是字符串,也可以是数组 // 将字符串中所有出现的元音使用空串替换掉 // 赋值: Hll Wrld f PHP $vowels = array(“a”, “e”, “i”, “o”, “u”, “A”, “E”, “I”, “O”, “U”); $onlyconsonants = str_replace($vowels, “”, “Hello World of PHP”); echo $onlyconsonants; echo “
    “;

    // 当查找值和替换值都使用数组,则值会一一对应替换, // 赋值: You should eat pizza, beer, and ice cream every day $phrase = “You should eat fruits, vegetables, and fiber every day.”; $healthy = array(“fruits”, “vegetables”, “fiber”); $yummy = array(“pizza”, “beer”, “ice cream”);

    $newphrase = str_replace($healthy, $yummy, $phrase); echo $newphrase; echo “
    “;

    // 赋值: 2 $str = str_replace(“ll”, “”, “good golly miss molly!”, $count); echo $count; echo “
    “; echo $str;

?>

  1. - [str_ireplace](https://www.php.net/manual/zh/function.str-ireplace.php) — str_replace 的忽略大小写版本
  2. ```php
  3. str_ireplace(
  4. mixed $search,
  5. mixed $replace,
  6. mixed $subject,
  7. int &$count = ?
  8. ): mixed
  1. <?php
  2. header("content-type:text/html;charset=utf8");
  3. // 替换不会改变原字符串,会生成一个新字符串
  4. $old = "aa";
  5. $new = "--";
  6. $str = "aaBBAABBaaBBAABB";
  7. $newStr = str_ireplace($old, $new, $str);
  8. echo $str;
  9. echo "<br>";
  10. echo $newStr;
  11. echo "<br>";
  12. ?>
  • str_repeat — 重复一个字符串

    1. str_repeat(string $input, int $multiplier): string
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. echo str_repeat("Hello", 5);
    4. echo "<br>";
    5. ?>
  • substr_replace — 替换字符串的子串(根据指定的位置 进行替换)
    substr_replace() 在字符串 string 的副本中将由 start 和可选的 length 参数限定的子字符串使用 replacement 进行替换。

    1. substr_replace(
    2. mixed $string,
    3. mixed $replacement,
    4. mixed $start,
    5. mixed $length = ?
    6. ): mixed

    ```php $var = ‘ABCDEFGH:/MNRPQR/‘; echo “Original: $var


    \n”;

/ 这两个例子使用 “bob” 替换整个 $var。/ echo substr_replace($var, ‘bob’, 0) . “
\n”; echo substr_replace($var, ‘bob’, 0, strlen($var)) . “
\n”;

/ 将 “bob” 插入到 $var 的开头处。/ echo substr_replace($var, ‘bob’, 0, 0) . “
\n”;

/ 下面两个例子使用 “bob” 替换 $var 中的 “MNRPQR”。/ echo substr_replace($var, ‘bob’, 10, -1) . “
\n”; echo substr_replace($var, ‘bob’, -7, -1) . “
\n”;

/ 从 $var 中删除 “MNRPQR”。/ echo substr_replace($var, ‘’, 10, -1) . “
\n”;

  1. <a name="75be37d2"></a>
  2. #### 8. 转换相关
  3. - [chr](https://www.php.net/manual/zh/function.chr.php) — 返回指定的字符
  4. ```php
  5. chr(int $ascii): string
  1. <?php
  2. echo "48 - >" . chr(48) . "<br>";
  3. echo "65 - >" . chr(65) . "<br>";
  4. echo "97 - >" . chr(97) . "<br>";
  5. ?>
  • ord — 转换字符串第一个字节为 0-255 之间的值

    1. ord(string $string): int
    1. <?php
    2. echo "0 - >" . ord("0") . "<br>";
    3. echo "A - >" . ord("A") . "<br>";
    4. echo "a - >" . ord("a") . "<br>";
    5. echo "ab - >" . ord("ab") . "<br>"; // 只转换字符串的第一个字符
    6. ?>
  • ucfirst — 将字符串的首字母转换为大写

    1. ucfirst(string $str): string

    ```php <?php $foo = ‘hello world!’; $foo = ucfirst($foo); // Hello world!

$bar = ‘HELLO WORLD!’; $bar = ucfirst($bar); // HELLO WORLD! $bar = ucfirst(strtolower($bar)); // Hello world! ?>

  1. - [ucwords](https://www.php.net/manual/zh/function.ucwords.php) — 将字符串中每个单词的首字母转换为大写
  2. ```php
  3. ucwords(string $str, string $delimiters = " \t\r\n\f\v" ): string
  1. <?php
  2. $foo = 'hello world!';
  3. $foo = ucwords($foo); // Hello World!
  4. echo $foo . "<br>";
  5. $foo = 'HELLO WORLD!';
  6. $foo = ucwords($foo); // HELLO WORLD!
  7. $foo = ucwords(strtolower($foo)); // Hello World!
  8. echo $foo . "<br>";
  9. $foo = 'hello|world!';
  10. $foo = ucwords($foo); // Hello|world!
  11. echo $foo . "<br>";
  12. $foo = ucwords($foo, "|"); // Hello|World!
  13. echo $foo . "<br>";
  14. ?>
  • lcfirst — 使一个字符串的第一个字符小写
    1. lcfirst(string $str): string
    ```php <?php $foo = ‘HelloWorld’; $foo = lcfirst($foo); // helloWorld

$bar = ‘HELLO WORLD!’; $bar = lcfirst($bar); // hELLO WORLD! $bar = lcfirst(strtoupper($bar)); // hELLO WORLD! ?>

  1. - [strtolower](https://www.php.net/manual/zh/function.strtolower.php) — 将字符串转化为小写
  2. ```php
  3. strtolower(string $string): string
  1. <?php
  2. $str = "Mary Had A Little Lamb and She LOVED It So";
  3. $str = strtolower($str);
  4. echo $str; // 打印 mary had a little lamb and she loved it so
  5. ?>
  • strtoupper — 将字符串转化为大写

    1. strtoupper(string $string): string
    1. <?php
    2. $str = "Mary Had A Little Lamb and She LOVED It So";
    3. $str = strtoupper($str);
    4. echo $str; // 打印 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
    5. ?>
  • strip_tags — 从字符串中去除 HTML 和 PHP 标记

    1. strip_tags(string $str, string $allowable_tags = ?): string
    1. <?php
    2. $text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
    3. echo strip_tags($text);
    4. // 允许 <p> 和 <a>
    5. echo strip_tags($text, '<p><a>');
    6. ?>
  • str_shuffle — 随机打乱一个字符串

    1. str_shuffle(string $str): string
    1. <?php
    2. $s = "Hello World";
    3. echo $s . "<br>";
    4. echo str_shuffle($s);
    5. ?>
  • htmlspecialchars — 将特殊字符转换为 HTML 实体

    1. htmlspecialchars(
    2. string $string,
    3. int $flags = ENT_COMPAT | ENT_HTML401,
    4. string $encoding = ini_get("default_charset"),
    5. bool $double_encode = true
    6. ): string

    | 字符 | 替换后 | | —- | —- | | &
    (& 符号) | & | | "
    (双引号) | "
    ,除非设置了 **ENT_NOQUOTES** | | '
    (单引号) | 设置了 **ENT_QUOTES**
    后, '
    (如果是 **ENT_HTML401**
    ) ,或者 '
    (如果是 **ENT_XML1**
    **ENT_XHTML**
    **ENT_HTML5**
    )。 | | <
    (小于) | < | | >
    (大于) | > |

  1. <?php
  2. $s = "<a href='test'>Test</a>";
  3. $new = htmlspecialchars($s, ENT_QUOTES);
  4. echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
  5. echo "<br>";
  6. echo $s;
  7. ?>

9. 分割连接

  • substr — 返回字符串的子串

    1. substr(string $string, int $offset, ?int $length = null): string
    1. <?php
    2. echo substr("abcdef", 3) . "<br>"; // 返回 "def"
    3. echo substr("abcdef", 0, -1) . "<br>"; // 返回 "abcde"
    4. echo substr("abcdef", 2, -1) . "<br>";; // 返回 "cde"
    5. echo substr("abcdef", 4, -4) . "<br>";; // 返回 ""; 在 PHP 8.0.0 之前,返回 false
    6. echo substr("abcdef", -3, -1) . "<br>";; // 返回 "de"
    7. ?>
  • str_split — 将字符串转换为数组

    1. str_split(string $string, int $split_length = 1): array
    1. <?php
    2. $str = "Hello Friend";
    3. $arr1 = str_split($str);
    4. $arr2 = str_split($str, 3);
    5. print_r($arr1);
    6. print_r($arr2);
    7. ?>
  • explode — 使用一个字符串分割另一个字符串

    1. explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
    1. <?php
    2. // 示例 1
    3. $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
    4. $pieces = explode(" ", $pizza);
    5. echo $pieces[0]; // piece1
    6. echo '<br>';
    7. echo $pieces[1]; // piece2
    8. echo '<br>';
    9. // 示例 2
    10. $data = "foo:*:1023:1000::/home/foo:/bin/sh";
    11. $arr = explode(":", $data);
    12. var_dump($arr);
    13. ?>
  • implode — 将一个一维数组的值转化为字符串

    1. implode(string $glue, array $pieces): string
    2. implode(array $pieces): string

    ```php <?php $array = array(‘lastname’, ‘email’, ‘phone’);

    echo implode($array); echo ‘
    ‘;

    echo implode(“,”, $array); echo ‘
    ‘;

    echo implode(“—“, $array); echo ‘
    ‘;

    // Empty string when using an empty array: var_dump(implode(‘hello’, array())); // string(0) “”

?>

  1. - [join](https://www.php.net/manual/zh/function.join.php) — 别名 implode
  2. - [strstr](https://www.php.net/manual/zh/function.strstr.php) — 查找字符串的首次出现, 返回 `haystack` 字符串从 `needle` 第一次出现的位置开始到 `haystack` 结尾的字符串。
  3. ```php
  4. <?php
  5. header("content-type:text/html;charset=utf8");
  6. $s = "Hello php hello mysql";
  7. echo strstr($s, "hello");
  8. echo "<br>";
  9. echo strstr($s, "php");
  10. echo "<br>";
  11. ?>
  • stristr — strstr 函数的忽略大小写版本

    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. $s = "Hello php hello mysql";
    4. echo stristr($s, "hello");
    5. echo "<br>";
    6. echo stristr($s, "php");
    7. echo "<br>";
    8. ?>
  • strchr — 别名 strstr

10. 去除空白

  • ltrim — 删除字符串开头的空白字符(或其他字符)

    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. $s1 = " hello world ";
    4. echo "|" . $s1 . "|";
    5. echo "<br>";
    6. echo "|" . ltrim($s1) . "|";
    7. echo "<br>";
    8. ?>
  • rtrim — 删除字符串末端的空白字符(或者其他字符)

    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. $s1 = " hello world ";
    4. echo "|" . $s1 . "|";
    5. echo "<br>";
    6. echo "|" . rtrim($s1) . "|";
    7. echo "<br>";
    8. ?>
  • trim — 去除字符串首尾处的空白字符(或者其他字符)

    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. $s1 = " hello world ";
    4. echo "|" . $s1 . "|";
    5. echo "<br>";
    6. echo "|" . trim($s1) . "|";
    7. echo "<br>";
    8. ?>

11. 加密

  • md5 — 计算字符串的 MD5 散列值
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. $s1 = "heLloworld";
    4. echo $s1;
    5. echo "<br>";
    6. echo md5($s1);
    7. echo "<br>";
    8. ?>

12. 反转字符串

  • strrev — 反转字符串
    1. <?php
    2. header("content-type:text/html;charset=utf8");
    3. $s1 = "heLloworld";
    4. echo $s1;
    5. echo "<br>";
    6. echo strrev($s1);
    7. echo "<br>";
    8. ?>

13. 序列化 serialize()

serialize() 返回字符串,此字符串包含了表示 value 的字节流,可以存储于任何地方。

这有利于存储或传递 PHP 的值,同时不丢失其类型和结构。

  1. serialize(mixed $value): string
  • 基本使用

    1. <?php
    2. // 序列化数组
    3. $animals = ["chicken", "duck", "goose",'dog','pig'];
    4. $aniStr = serialize($animals);
    5. echo $aniStr;
    6. echo "<br>";
    7. // 序列化复杂数组
    8. $animals = ["chicken", "duck", "goose",'dog','pig',100,true,[1,2,3]];
    9. $aniStr = serialize($animals);
    10. echo $aniStr;
    11. echo "<hr>";
    12. // 基本数据类型也可以进行序列化,但一般没必要
    13. echo serialize(100) . "<br>"; //i:100;
    14. echo serialize(3.14) . "<br>"; //d:3.140000000000000124344978758017532527446746826171875;
    15. echo serialize(true) . "<br>"; //b:1;
    16. echo serialize(false) . "<br>"; //b:0;
    17. echo serialize("hello") . "<hr>"; //s:5:"hello";
    18. ?>
  • 序列化原理
    ```php <?php function mySerialize($data){ $s = “;”;

    1. // 用来保存序列化字符串的变量

    $retS = “”;

    1. // 获取类型

    $t = gettype($data);

    1. // 根据不同的类型进行不同形式的拼接

    switch($t){

    1. // 整数类型
    2. case "integer":
    3. $retS .= "i:" . $data;
    4. break;
    5. // 小数类型
    6. case "double":
    7. $retS .= "d:" . $data;
    8. break;
    9. // 布尔类型
    10. case "boolean":
    11. $v = $data?"1":"0";
    12. $retS .= "b:" . $v;
    13. break;
    14. // 字符串类型
    15. case "string":
    16. $l = strlen($data);
    17. $retS .= "s:{$l}:" . "\"$data\"";
    18. break;
    19. // 数组类型
    20. case "array":
    21. $retS .= "a:" . count($data) . ":{";
    22. foreach ($data as $key => $value) {
    23. $retS .= mySerialize($key);
    24. $retS .= mySerialize($value);
    25. }
    26. $retS .= "}";
    27. // 数组后面不拼接分号
    28. $s = "";
    29. break;

    } return $retS . $s; }

echo mySerialize(100) . “
“; //i:100; echo mySerialize(3.14) . “
“; //d:3.140000000000000124344978758017532527446746826171875; echo mySerialize(true) . “
“; //b:1; echo mySerialize(false) . “
“; //b:0; echo mySerialize(“hello”) . “
“; //s:5:”hello”; // 序列化复杂数组 $animals = [“chicken”, “duck”, “goose”,’dog’,’pig’,100,3.14,true,[1,2,3]]; $aniStr = mySerialize($animals); echo $aniStr; echo “


“; ?>

  1. <a name="15958dae"></a>
  2. #### 14. 反序列化
  3. **unserialize()** 对单一的已序列化的变量进行操作,将其转换回 PHP 的值。
  4. ```php
  5. unserialize(string $str): mixed
  • 序列化数据

    1. i:100;
    2. d:3.14;
    3. b:1;
    4. b:0;
    5. s:5:"hello";
    6. a:9:{i:0;s:7:"chicken";i:1;s:4:"duck";i:2;s:5:"goose";i:3;s:3:"dog";i:4;s:3:"pig";i:5;i:100;i:6;b:1;i:7;a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}i:8;s:3:"php";}
  • 反序列化

    1. <?php
    2. $file = fopen("test.txt","r");
    3. do{
    4. $content = fgets($file);
    5. $data = unserialize($content);
    6. var_dump($data);
    7. }while($content);
    8. fclose($file);
    9. echo "<hr>";