关于php:PHP基础二常用字符串处理函数

4次阅读

共计 5029 个字符,预计需要花费 13 分钟才能阅读完成。

  • chr(int $ascii) : string 依据指定 ASCII 值返回指定的字符。
$a = chr(65);
echo $a; // A
  • echo(string $arg1, string $...) : void 输入一个或多个字符串,echo 不是一个函数(只是一个语言构造)
  • explode(string $delimiter, string $string, int $limit = ?) : array返回一个由 $delimiter宰割 $string之后产生的子串组成的数组。如果设置了 $limit参数,则返回的数组里蕴含最多 $limit个元素,其中最初的那个元素将蕴含 string的残余局部。
$a = 'Hello World,This is my house!';
$b = explode(' ', $a);
print_r($a);// Array ([0] => Hello [1] => World,This [2] => is [3] => my [4] => house! )

$c = explode(' ', $a, 3);
print_r($c);// Array ([0] => Hello [1] => World,This [2] => is my house! )
  • fprintf(resource $handle, string $format, mixed $... = ?) : int写入一个经 $format格式化后的字符串到由 fopen关上的资源 $handle中去,返回写入的字符串的长度。
$year = date('Y');
$month = date('m');
$day = date('d');
$file = fopen('./a.txt','w+');
fprintf($file, '%04d-%02d-%02d', $year, $month, $day);
  • htmlspecialchars(string $string, int $flags = ENT_COMPAT | ENT_HTML401, string $encoding = ini_get("default_charset"), bool $double_encode = true) : string 将特殊字符转换为 HTML 实体
  • implode(string $glue, array $pieces) : string$glue返回一个用 $glue将一维数组的值连成的一个字符串。glue默认为空
  • join,implode 函数的别名
  • lcfirst(string $str) : string返回将字符串的第一个字母转为小写后的新字符串。
$a = 'Hello';
echo lcfirst($a);//hello
  • ltrim(string $str, string $character_mask = ?) : string 删除字符串结尾的空白字符(或其余指定的字符),并返回
  • md5(string $str, bool $raw_output = false) : string计算字符串的 MD5 散列值
  • number_format(float $number, int $decimals = 0, string $dec_point = ".", string $thousands_sep = ",") : string 格式化数字。如果只提供第一个参数,number 的小数局部会被去掉并且每个千位分隔符都是英文小写逗号 ”,”;如果提供两个参数,number 将保留小数点后的位数到你设定的值,其余同楼上;如果提供了四个参数,number 将保留 decimals 个长度的小数局部, 小数点被替换为 dec_point,千位分隔符替换为 thousands_sep
  • ord(string $string) : int返回指定字符的 ASCII 值。同 chr() 函数相同操作。
  • print(string $arg) : int print 实际上也不是函数,所以能够不必圆括号突围参数列表。其和 echo的区别是 echo 能够有多个参数,而 print 只能有一个参数,且 print 总是返回 1。
  • printf(string $format, mixed $args = ?, mixed $... = ?) : int输入格式化后的字符串。
  • rtrim(string $str, string $character_mask = ?) : string删除 $str 末端的空白字符,并返回。
  • str_contains(string $haystack, string $needle) : bool查找字符串 $haystack 中是否蕴含字符串$needle,蕴含则返回true,否则返回false(PHP >= 8.0)
  • str_ends_with(string $haystack, string $needle) : bool查看字符串 $haystack 是否以字符串 $needle 结尾,是则返回true,否则返回false(PHP >= 8.0)
  • str_ireplace(mixed $search, mixed $replace, mixed $subject, int &$count = ?) : mixed函数返回一个字符串或者数组。该字符串或者数组是将 subject 中全副 search 都被 replace 替换掉当前的后果,不辨别大小写。str_replace()是此函数辨别大小写的版本。
$a = "Hello world!This is my sister,Ella.";
$str = str_ireplace('e','E',$a);
echo $str;//HEllo world!This is my sistEr,Ella.

$b = ['ella','jason','jack','musk'];
$b = str_ireplace('a','aa',$b);
print_r($b);//Array ([0] => ellaa [1] => jaason [2] => jaack [3] => musk )
  • str_pad(string $input, int $pad_length, string $pad_string = " ", int $pad_type = STR_PAD_RIGHT) : string 将字符串 $input 从左边(默认左边,能够抉择右边或两端)填充到指定长度$pad_length,如果没有指定填充的字符串$pad_string,则默认应用空格填充。
$str = "A";

$b = str_pad($str, 10);
echo strlen($b); // 10

$c = str_pad($str,10,'a');
echo $c; //Aaaaaaaaaa

$c = str_pad($str,10,'a', STR_PAD_LEFT);
echo $c; //aaaaaaaaaA

$c = str_pad($str,10,'a', STR_PAD_BOTH);
echo $c; // aaaaAaaaaa
  • str_repeat(string $input, int $multiplier) : string 返回 $input 反复 $multiplier 次之后的后果。如果 multiplier 为 0,则返回空字符串。
$str = 'Hello';
$a = str_repeat($str,2);
$b = str_repeat($str,0);
echo $a; //HelloHello
echo $b; // 
  • str_split(string $string, int $split_length = 1) : array将一个字符串转换为数组,数组中的每个元素的长度由 $split_length(大于 0 的整数) 指定。当 $split 参数不指定时,则默认为单个字符组成一个元素。
$str = 'hello';
$b = str_split($str);
print_r($b); // Array ([0] => h [1] => e [2] => l [3] => l [4] => o ) 

$c = str_split($str, 3);
print_r($c); // Array ([0] => hel [1] => lo )
  • str_starts_with(string $haystack, string $needle) : bool 判断字符串 $haystack 是否以字符串 $needle 结尾。(PHP >= 8.0)
  • stripos(string $haystack, string $needle, int $offset = 0) : int 查找 $needle 在字符串 $haystack 中首次呈现的地位,不辨别大小写。如果未发现则返回 false。stripos()函数是其辨别大小写的版本。
$str = "hello world";
$a = stripos($str, 'wo');
echo $a; // 6

$b = stripos($str, 'A');
var_dump($b); // bool(false)
  • stristr(string $haystack, mixed $needle, bool $before_needle = false) : string返回 $haystack 字符串从 $needle 第一次呈现的地位开始到结尾的字符串,不辨别大小写。strstr是其辨别大小写的版本。
$str = "hello world";
$a = stristr($str, 'wo');
echo $a; //world

$b = strstr($str,'Wo');
var_dump($b); // bool(false)
  • strlen(string $string) : int 返回给定字符串的长度
  • strrchr(string $haystack, mixed $needle) : string函数返回 haystack字符串中的一部分,这部分以 needle的最初呈现地位开始,直到 haystack 开端,辨别大小写。不同于 strstr 的是,这个函数中,如果 $needle 参数有多个字符,则只应用第一个字符。
$str = "hello wrld";
$a = strrchr($str, 'wo');
echo $a; // wrld
  • strrev(string $string) : string 反转字符串。
$str = 'hello world';
$b = strrev($str);
echo $b; // dlrow olleh
  • strripos(string $haystack, string $needle, int $offset = 0) : int返回 $haystack$needle最初一次呈现的地位,不辨别大小写。函数 strrpos() 与此函数性能雷同,区别是辨别大小写。
$str = "hello world";
$a = strripos($str, 'wo');
echo $a; // 6
  • strtolower(string $string) : string 将字符串中所有字符都转为小写,并返回
  • strtoupper(string $string) : string 将字符串中所有字符都转为大写,并返回
  • substr_count(string $haystack, string $needle, int $offset = 0, int $length = ?) : int函数返回子字符串 $needle 在字符串 $haystack 中呈现的次数,辨别大小写。
$str = "hello world";
$a = substr_count($str, 'wo');
echo $a; // 1
$b = substr_count($str, 'Wo');
echo $b; // 0
  • substr(string $string, int $start, int $length = ?) : string返回指定字符串的子字符串。
$str = 'Hello World';
$a = substr($str, 2, 10);
echo $a; // llo World
  • trim(string $str, string $character_mask = "\t\n\r\0\x0B") : string返回字符串 $string 两端去除首位空白字符或指定字符后的后果。
  • ucfirst(string $str) : string 将字符串首字母转为大写,并返回
  • ucwords(string $str, string $delimiters = "\t\r\n\f\v") : string将字符串中每个单词的首字母转为大写字母,并返回。
  • wordwrap(string $str, int $width = 75, string $break = "\n", bool $cut = false) : string打断字符串为指定数量的字串
$str = 'Hello World! this is my house!';
$a = wordwrap($str, 3,'');
var_dump($a); // string(30) "Hello\World!\this\is\my\house!"
正文完
 0