php array and string conversion function collation summary

  • 2020-07-21 07:17:24
  • OfStack

1. Converts a string to an array
str_split() is used to convert a string to an array
Grammar:

str_split(string,length)
<SPAN style="COLOR: #333333"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-FAMILY:  Song typeface ">//</SPAN></SPAN></SPAN>string Is required, is a string to be split; <SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-FAMILY:  Song typeface ; COLOR: #333333"><SPAN style="LINE-HEIGHT: 28px">
//length Is optional and specifies the length of each array element 
</SPAN></SPAN></SPAN>

tips:
If length is less than 1, the str_split() function returns false.
If length is greater than the length of the string, the entire string is returned as the only element of the array.
Example:

<?php
$str="www.baidu.com";
print_r(str_split($str));
?>

2. String split function
The explode() function splits the string into arrays.
Grammar:
explode(separator,string,limit)
//separator is required and specifies the basis for splitting a string, e.g. "" (space)" "|" ", "etc
//string is required and is the string to be manipulated
//limit is optional and specifies the maximum number of array elements to return.
Example:

<?php
$types="doc|docx|ppt|pptx|xls|xlsx|zip|rar";
print_r(explode("|",$types));
?>

3. Convert 1 array to string
Combine the array elements into a string using the implode() function
Grammar:

<PRE>implode(separator,array)</PRE>//seperator Is optional, specifies what to place between array elements, default is "" (empty string) <BR>
//array Is a must, an array to be combined into a string <BR>
<PRE></PRE>
tips: although  <EM>separator</EM>  The parameters are optional. However, for backward compatibility, it is recommended that you use two parameters. implode()  Two parameter orders can be received. But for historical reasons, explode()  No. You have to promise 
<EM>separator</EM>  Parameters in  <EM>string</EM>  Before the parameters. Example: <PRE class=php name="code"><?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?></PRE><BR>
<PRE></PRE>

4. Find another character in a string type
Use the strpos() or strstr() functions
The strpos() function returns the position of the first occurrence of a string in another string.
If the string is not found, false is returned.
Grammar:

strpos(string,find,start)
//string Must, represents the string being searched for 
//find Must, represents the string being looked for 
//start Optional. Specify where to start the search. 

tip:
This function is case sensitive
Example:

<?php
$str="HellO neo";
$find1="O";
$find2="o";
echo strpos($str,$find1);
echo "<br/>";
echo strpos($str,$find2);
?>

Output results:

4
8

Intercept part of a string
The substr() function returns part 1 of the string
Grammar:

substr(string,start,length)
//string Is required to return to it 1 Part of a string. 
/* A necessity. Specifies where to start a string. 
     A positive number  -  Begins at the specified position in the string 
     A negative number  -  Begins at the specified position from the end of the string 
    0 -  The first in a string 1 Start with two characters 
*/
/* Optional. Specifies the length of the string to return. The default is up to the end of the string. 
     A positive number  -  from  start  Returns the location of the parameter 
     A negative number  -  Returns from the end of a string 
*/

tips:
If start is negative and length is less than or equal to start, length is 0.
Example;

<?php
$str="Hello world!";
echo substr($str,0);
echo "<br/>";
echo substr($str,6,5);
?>

6. Gets the length of the string
The strlen() function is used to calculate the length of a string.
Example:

<?php
$str="Hello world!";
echo strlen($str);
?>
// The output result is 12

Converts the string to uppercase
The strtoupper() function converts a string to uppercase.
Example:

<?php
$str="www.baidu.com";
print_r(str_split($str));
?>
0
8. Converts strings to lowercase
The strtolower() function converts a string to lowercase.
Example:

<?php
$str="Hello World!";
echo strtolower($str);
?>

Related articles: