The following is a detailed summary of the conversion functions between arrays and strings in php. Friends in need can refer to the following
1. Convert a string into an array
str_split() is used to convert a string into an array
Syntax:
str_split(string,length) <SPAN style="COLOR: #333333"><SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-FAMILY: 宋体">//</SPAN></SPAN></SPAN>string是必须的,是要分割的字符串;<SPAN style="FONT-SIZE: 12px"><SPAN style="FONT-FAMILY: 宋体; COLOR: #333333"><SPAN style="LINE-HEIGHT: 28px"> //length是可选的,规定每个数组元素的长度 </SPAN></SPAN></SPAN>
tips:
If length is less than 1, the str_split() function will return false.
If length is greater than the length of the string, the entire string will be returned as the only element of the array.
Example:
<?php $str="www.baidu.com"; print_r(str_split($str)); ?>
2. String splitting function
explode() function splits the string is an array.
Syntax:
explode(separator,string,limit)
//separator is required and specifies the basis for splitting the string, for example: " "(space) "| " "," etc.
//string is required and is the string to be operated on
//limit is optional and specifies the maximum number of array elements returned.
Example:
<?php $types="doc|docx|ppt|pptx|xls|xlsx|zip|rar"; print_r(explode("|",$types)); ?>
3. Convert an array into a string
Use the implode() function Combine array elements into a string
Syntax:
<PRE>implode(separator,array)//seperator是可选的,规定数组元素之间放置的内容,默认是“”(空字符串)
<?php $arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr); ?>
4. Find another character in the string
Use strpos() or strstr() function
The strpos() function returns the position of the first occurrence of a string in another string.
If the string is not found, return false.
Syntax:
strpos(string,find,start) //string为必须,表示被搜索的字符串 //find为必须,表示被查找的字符串 //start可选。规定开始搜索的位置。
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 result:
4
8
## 5. Intercept part of the characters in the string
substr() function returns part of the string
Syntax:
substr(string,start,length) //string为必需,规定要返回其中一部分的字符串。 /*必需。规定在字符串的何处开始。 正数 - 在字符串的指定位置开始 负数 - 在从字符串结尾的指定位置开始 0 - 在字符串中的第一个字符处开始 */ /*可选。规定要返回的字符串长度。默认是直到字符串的结尾。 正数 - 从 start 参数所在的位置返回 负数 - 从字符串末端返回 */
tips:
If start is a negative number 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. Get the string length
strlen() function is used to calculate characters The length of the string.
Example:
<?php $str="Hello world!"; echo strlen($str); ?>
7. Convert the string to uppercase
strtoupper() function converts a string to uppercase.
Example:
<?php $str="Hello world!"; echo strtoupper($str); ?> //输出结果为:HELLO WORLD!
The above is the detailed content of Summary of usage of PHP array and string conversion functions. For more information, please follow other related articles on the PHP Chinese website!
8. Convert the string to lowercase
strtolower## The #() function converts a string to lowercase. Example:<?php
$str="Hello World!";
echo strtolower($str);
?>