Home  >  Article  >  PHP string function (3): conversion between string and array

PHP string function (3): conversion between string and array

无忌哥哥
无忌哥哥Original
2018-06-28 10:42:103223browse
Recommended manual:php complete self-study manual

* 1.str_split($str,$length=1), according to the number of characters, Split the string into an array, the default is 1

* 2.explode($delimiter,$str,$num): Split the string into an array according to the delimiter, the number of array elements can be specified

* 3.implode($glue, $str): Assemble the one-dimensional array into a string according to the delimiter, separated by spaces by default

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
$str = &#39;html,css,jquery,php,mysql,thinkphp&#39;;
echo &#39;<p>原始字符串:&#39;.$str.&#39;</p>&#39;;

1.str_split($str,$length=1) Split the string into an array according to the number of characters, the default is 1

print_r(str_split($str)); //默认一个字符转为数组中的一个元素
print_r(str_split($str,5)); //5个一组进行转换

2.explode($delimiter,$str,$num): Split the string into an array according to the delimiter, you can Specify the number of array elements

print_r(explode(&#39;,&#39;,$str)); //用&#39;,&#39;号进行分割字符串
print_r(explode(&#39;,&#39;,$str,5)); //指定数组必须是5个元素,最后一个元素保存全部剩余数据

3.implode($glue, $str): Assemble the one-dimensional array into a string according to the delimiter, separated by spaces by default

$arr2 = explode(&#39;,&#39;, $str);
echo implode(&#39; &#39;, $arr2), &#39;<br>&#39;; //用空格分隔
echo implode(&#39;,&#39;,$arr2), &#39;<br>&#39;; //用,分隔
echo implode(&#39;--&#39;,$arr2), &#39;<br>&#39;; //用--分隔
Recommended related articles:
1.Convert PHP arrays and strings to each other
2.Detailed explanation of the two methods of converting PHP arrays to strings [Video attached]
3.Explanation of PHP string to array and array to string functions
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn