PHP Foundation (2) commonly used string processing functions

Time:2021-4-9
  • chr(int $ascii) : stringReturns the specified character according to the specified ASCII value.
$a = chr(65);
echo $a; // A
  • echo(string $arg1, string $...) : voidOutput one or more strings, echo is not a function (just a language structure)
  • explode(string $delimiter, string $string, int $limit = ? ) : arrayReturns a$delimiterdivision$stringAfter that, an array of substrings is generated. If set$limitParameter, the returned array contains the most$limitThe last element will containstringThe rest of the project.
$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 $... = ?) : intWrite a classic$formatFrom the formatted string to thefopenOpen resources$handleReturns the length of the string to be written.
$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) : stringConverting special characters to HTML entities
  • implode(string $glue, array $pieces) : stringuse$glueReturn a$glueA string that concatenates the values of a one-dimensional array.glueIt is empty by default
  • join, alias of implode function
  • lcfirst(string $str) : stringReturns a new string that converts the first letter of a string to lowercase.
$a = 'Hello';
echo lcfirst($a);//hello
  • ltrim( string $str, string $character_mask = ?) : stringRemoves the white space character (or other specified character) at the beginning of the string and returns the
  • md5( string $str, bool $raw_output = false) : stringComputes the MD5 hash value of a string
  • number_format( float $number, int $decimals = 0, string $dec_point = ".", string $thousands_sep = ",") : stringFormat numbers. If only the first parameter is provided, the decimal part of number will be removed, and each thousand separator is a comma in English; if two parameters are provided, number will keep the number of decimal places to the value you set, and the rest are the same; if four parameters are provided, number will keep the decimal parts of decimals, and the decimal point will be replaced by Dec_ Point, thousand separator replaced with thousands_ sep
  • ord(string $string) : intReturns the ASCII value of the specified character. withchr()Functions operate in reverse.
  • print(string $arg) : intPrint is not actually a function, so you don’t need to surround the parameter list with parentheses. Its sumechoWhat’s the differenceechoThere can be multiple parameters, andprintThere can only be one parameter, andprintAlways returns 1.
  • printf(string $format, mixed $args = ?, mixed $... = ?) : intOutput the formatted string.
  • rtrim(string $str, string $character_mask = ?) : stringdelete$strThe space character at the end of the string and returns.
  • str_contains( string $haystack, string $needle) : boolFind string$haystackWhether the string is included in the$needleIf it containstrueOtherwise, returnfalse(PHP >= 8.0)
  • str_ends_with(string $haystack, string $needle) : boolCheck string$haystackIs it a string$needleAt the end, if yes, returntrueOtherwise, returnfalse(PHP >= 8.0)
  • str_ireplace(mixed $search, mixed $replace, mixed $subject, int &$count = ?) : mixedFunction returns a string or array. The string or array is thesubjectAll insearchAll byreplaceReplace the result after, not case sensitive.str_replace()Is the case sensitive version of this function.
$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) : stringString$inputFill from the right side (default right, left or both ends can be selected) to the specified length$pad_length, if no padding string is specified$pad_string, the default is to fill with spaces.
$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) : stringreturn$inputrepeat$multiplierThe result after the first time. IfmultiplierIs 0, an empty string is returned.
$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) : arrayConverts a string to an array in which the length of each element is determined by the$split_ Length (integer greater than 0)appoint. When$splitIf the parameter is not specified, a single character will form an element by default.
$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) : boolJudgment string$haystackIs it a string$needlestart. (PHP >= 8.0)
  • stripos( string $haystack, string $needle, int $offset = 0) : intlookup$needleIn string$haystackFirst occurrence in, case insensitive. False if not found.stripos()Function is its case sensitive version.
$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) : stringreturn$haystackString from$needleThe first occurrence of a string from the beginning to the end, case insensitive.strstrIs its case sensitive version.
$str = "hello world";
$a = stristr($str, 'wo');
echo $a; //world

$b = strstr($str,'Wo');
var_dump($b); // bool(false)
  • strlen( string $string) : intReturns the length of the given string
  • strrchr( string $haystack, mixed $needle) : stringFunction returnhaystackThe part of a string that is represented by theneedleStart at the last position untilhaystackAt the end, case sensitive. differstrstrWhat’s important is that in this function, if$needleIf the parameter has more than one character, only the first character is used.
$str = "hello wrld";
$a = strrchr($str, 'wo');
echo $a; // wrld
  • strrev( string $string) : stringInverts the string.
$str = 'hello world';
$b = strrev($str);
echo $b; // dlrow olleh
  • strripos( string $haystack, string $needle, int $offset = 0) : intreturn$haystackin$needleLast occurrence position, case insensitive. functionstrrpos()It has the same function as this function except that it is case sensitive.
$str = "hello world";
$a = strripos($str, 'wo');
echo $a; // 6
  • strtolower( string $string) : stringConverts all characters in a string to lowercase and returns the
  • strtoupper( string $string) : stringConverts all characters in a string to uppercase and returns the
  • substr_count( string $haystack, string $needle, int $offset = 0, int $length = ?) : intFunction returns a substring$needleIn string$haystackNumber of occurrences in, case sensitive.
$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 = ?) : stringReturns a substring of the specified string.
$str = 'Hello World';
$a = substr($str, 2, 10);
echo $a; // llo World
  • trim( string $str, string $character_mask = " \t\n\r\0\x0B") : stringReturn string$stringThe result of removing the first white space character or the specified character at both ends.
  • ucfirst( string $str) : stringConverts the initial of a string to uppercase and returns the
  • ucwords( string $str, string $delimiters = " \t\r\n\f\v" ) : stringConverts the first letter of each word in a string to uppercase and returns.
  • wordwrap( string $str, int $width = 75, string $break = "\n", bool $cut = false) : stringThe break string is the specified number of strings
$str = 'Hello World! this is my house!';
$a = wordwrap($str, 3,'');
var_dump($a); // string(30) "Hello\World!\this\is\my\house!"