欢迎访问宙启技术站
智能推送

10个PHP函数:从字符串中提取子字符串

发布时间:2023-09-16 05:26:46

1. substr(): 从一个字符串中提取子字符串。

例如:$str = "Hello, world!";

      echo substr($str, 7, 5);

      // 输出:world

2. mb_substr(): 从一个多字节字符串中提取子字符串。

例如:$str = "你好,世界!";

      echo mb_substr($str, 3, 2);

      // 输出:世界

3. strstr(): 在一个字符串中查找 次出现的子字符串,并返回从该子字符串开始到字符串结尾的部分。

例如:$str = "Hello, world!";

      echo strstr($str, "world"); 

      // 输出:world!

4. stristr(): 在一个字符串中查找 次出现的子字符串(不区分大小写),并返回从该子字符串开始到字符串结尾的部分。

例如:$str = "Hello, world!";

      echo stristr($str, "WORLD"); 

      // 输出:world!

5. substr_count(): 返回一个字符串中指定子字符串出现的次数。

例如:$str = "Hello, world!";

      echo substr_count($str, "o");

      // 输出:2

6. strpos(): 返回一个字符串中 次出现指定子字符串的位置(从0开始计数)。

例如:$str = "Hello, world!";

      echo strpos($str, "o");

      // 输出:4

7. stripos(): 返回一个字符串中 次出现指定子字符串的位置(不区分大小写)。

例如:$str = "Hello, world!";

      echo stripos($str, "O");

      // 输出:4

8. strrpos(): 返回一个字符串中最后一次出现指定子字符串的位置。

例如:$str = "Hello, world!";

      echo strrpos($str, "o");

      // 输出:7

9. strripos(): 返回一个字符串中最后一次出现指定子字符串的位置(不区分大小写)。

例如:$str = "Hello, world!";

      echo strripos($str, "O");

      // 输出:7

10. explode(): 将一个字符串按照指定分隔符拆分为数组。

例如:$str = "Hello, world!";

      $arr = explode(", ", $str);

      print_r($arr);

      // 输出:Array ( [0] => Hello [1] => world! )