Stringfunctions, a common character string function in PHP. This chapter describes several common internal functions of PHP strings. The following describes the internal functions of PHP strings: echo, print, strlen, trim, ltrim, rtrim, substr, strtolower, and st. This chapter describes several common internal functions of PHP strings. The internal functions of PHP strings described below include echo, print, strlen, trim, ltrim, rtrim, substr, strtolower, strtoupper, and str_replace.
Echo and print
For details, see the differences between PHP echo and print.
Strlen
The strlen function returns the length of a string. In the following example, the length of the variable $ a is 8.
$a = ' abcdef ';echo strlen($a); //8
Trim
The trim function removes spaces on both sides of the string. For example, in the following example, the value of the variable $ a is 'abcdef' and there is a space on both sides of the string. after the trim, the length of the string is 6 because the two spaces on both sides of the string are removed.
$a = ' abcdef ';echo strlen(trim($a)); //6
Ltrim
The ltrim function removes spaces on the left of the string.
echo 'nice',' try'; //nice tryecho 'nice',ltrim(' try'); //nicetry
Rtrim
The rtrim function removes spaces on the right of the string.
echo 'a ', 'b'; //a becho rtrim('a '),'b'; //ab
Substr
Use the substr function to obtain a part of the string. The substr function syntax is as follows:
substr(string,start,length)
It refers to intercepting a string with a length starting from the start position of the string. The first character of the string is 0, not 1. Example:
echo substr('blablar.com',0,3); //bla
The preceding example indicates that, starting from the first character of the string, three characters are truncated and the returned result is bla.
echo substr('blablar.com',3,5); //blar.
The preceding example indicates that the string blablar.com is truncated from the fourth character and the result is blar ..
You can also extract all the strings starting from the start position without writing the parameter length. for example:
echo substr('blablar.com', 3); //blar.com
Strtolower
The strtolower function is to convert all strings to lowercase letters. Example:
echo strtolower('BlaBlar.COM');//blablar.com
Strtoupper
Strtoupper is opposite to strtolower, which converts string to uppercase. Example:
echo strtoupper('china'); //CHINA
Str_replace
Str_replace is used to replace strings. The syntax of the str_replace function is as follows:
str_replace(search,replace,subject)
It means to locate any search-compliant string in the subject string and replace all search strings with replace.
Example:
echo str_replace("bla","CHA","blablar"); //CHACHAr
In the above example, use CHA to replace all bla in the blablar string, and the returned result is CHACHAr.