php常用内置函数-String

substr - 返回字符串的子串

  • substr(string $string, int $offset, ?int $length = null): string
      $str = 'hello world';
      echo substr($str, -1); //d
      echo substr($str, 2, 5); //llo w

mb_substr - 获取部分字符串

  • 定义
      mb_substr(
          string $str,
          int $start,
          int $length = NULL,
          string $encoding = mb_internal_encoding()
      ): string
      $str = '你好 世界';
      echo mb_substr($str, 0, 2, 'UTF-8'); //你好

strrev - 字符串反转

  • strrev(string $string): string

      $str = 'hello world';
      echo strrev($str); //dlrow olleh
  • 不使用内置函数的实现

      $str = 'hello world';
      $len = strlen($str);
    
      $rev_str = '';
      for ($i=$len;$i>=0;$i--) {
          $rev_str .= $str[$i];
      }
    
      echo $rev_str; //dlrow olleh

explode - 使用一个字符串分割另一个字符串

  • explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

      $str = 'hello world';
    
      print_r(explode(' ', $str));
          Array
          (
              [0] => hello
              [1] => world
          )

implode - 用字符串连接数组元素

  • implode(string $separator, array $array): string
      $arr = ['hello', 'world'];
      echo implode(' ', $arr); //hello world

str_replace - 子字符串替换

  • 定义
      str_replace(
          mixed $search,
          mixed $replace,
          mixed $subject,
          int &$count = ?
      ): mixed
      //count是指替换的次数,默认全部替换
      $str = 'hello world';
      echo str_replace('world', 'php', $str); //hello php

substr_count - 计算字串出现的次数

  • 定义
      substr_count(
          string $haystack,
          string $needle,
          int $offset = 0,
          ?int $length = null
      ): int
      $str = 'hello world';
      echo substr_count($str, 'o'); //2

trim - 去除字符串首尾处的空白字符(或者其他字符)

  • trim(string $str, string $character_mask = “ \t\n\r\0\x0B”): string
      $str = ' hello world ';
      echo trim($str); //hello world

strrpos — 计算指定字符串在目标字符串中最后一次出现的位置

  • strrpos — 计算指定字符串在目标字符串中最后一次出现的位置
      $str = 'test.xxx.jpg';
      echo strrpos($str, '.'); //8
      //获取文件类型
      echo substr($str, strrpos($str, '.')); //.jpg

preg_match - 执行匹配正则表达式

  • 定义
      preg_match(
          string $pattern,
          string $subject,
          array &$matches = null,
          int $flags = 0,
          int $offset = 0
      ): int|false
      $str = 'test.xxx.jpg';
      echo preg_match('/(\.)+/', $str, $matches); //1
      print_r($matches);
          Array
          (
              [0] => .
              [1] => .
          )

str_pad - 使用另一个字符串填充字符串到指定长度

  • 定义
      str_pad(
          string $input,
          int $pad_length,
          string $pad_string = " ",
          int $pad_type = STR_PAD_RIGHT
      ): string
      $str = '95';
      echo str_pad($str, 4, '19', STR_PAD_LEFT); //1995

strtolower — 将字符串转化为小写

  • strtolower(string $string): string
      $str = 'Hello World!';
      echo strtolower($str); //hello world!

strtoupper — 将字符串转化为大写

  • strtoupper(string $string): string
      $str = 'Hello World!';
      echo strtolower($str); //HELLO WORLD!

strstr - 查找字符串的首次出现

  • strstr(string $haystack, mixed $needle, bool $before_needle = false): string
      $str = 'xxx@gmail.com';
      echo strstr($str, '@'); //@gmail.com
      echo substr(strstr($str, '@'), 1); //gmail.com

htmlspecialchars - 将特殊字符转化为html实体

  • 定义
      htmlspecialchars(
          string $string,
          int $flags = ENT_COMPAT | ENT_HTML401,
          string $encoding = ini_get("default_charset"),
          bool $double_encode = true
      ): string
      $str = '<scritp>alert("s")</script>';
      echo htmlspecialchars($str); //&lt;scritp&gt;alert(&quot;s&quot;)&lt;/script&gt;

htmlspecialchars_decode - 将特殊的 HTML 实体转换回普通字符

  • htmlspecialchars_decode(string $string, int $flags = ENT_COMPAT | ENT_HTML401): string
      $str = '&lt;scritp&gt;alert(&quot;s&quot;)&lt;/script&gt;';
      echo htmlspecialchars_decode($str); //<scritp>alert("s")</script>
    

str_split - 将字符串转换为数组

  • str_split(string $string, int $split_length = 1): array
      $str = 'hello world';
      print_r(str_split($str, 4));
          Array
          (
              [0] => hell
              [1] => o wo
              [2] => rld
          )

number_format - 以千位分隔符方式格式化一个数字

  • number_format(float $number, int $decimals = 0): string
      $num = 1234567;
      echo number_format($num); //1,234,567
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1

my.oschina.net/u/135345/blog/55247... 我整理了一份string函数速览,便于查找。

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!