Home >Backend Development >PHP Tutorial >PHP function example: convert string to array
In the field of programming, strings and arrays are one of the most commonly used data types. A string can be a sequence of characters, while an array is a collection of elements arranged in a specific order. During development, we sometimes need to convert strings into arrays. At this time, PHP provides some built-in functions that can help us achieve this goal.
1. Use the split() function to convert a string into an array
PHP’s split() function is a function used to extract a substring from a given string and put it into Functions in arrays. The following is a sample code:
<?php //创建一个字符串 $str = "apple, orange, banana"; //用split()函数将字符串转换成数组 $arr = split(", ", $str); //输出结果 print_r($arr); ?>
The above code uses the split() function to convert a string containing three fruit names into an array. In this function, the first parameter specifies the pattern or regular expression to search for. In the example, we use a comma followed by a space as the delimiter pattern. The second parameter is the string to search for itself. Ultimately, the split() function returns an array in which each element is a portion of the original string separated by a comma followed by a space.
2. Use the explode() function to convert a string into an array
In PHP, the explode() function can also be used to split a string into an array. This function is similar to the split() function, but it does not support regular expressions, so be careful to choose the appropriate delimiter to split the string. The following is a sample code:
<?php //创建一个字符串 $str = "apple, orange, banana"; //用explode()函数将字符串转换成数组 $arr = explode(", ", $str); //输出结果 print_r($arr); ?>
In the above code, we use the explode() function to convert a string containing three fruit names into an array. In this function, the first parameter specifies where to split the string. In the example, we use a comma followed by a space as the separator. Then in the second parameter we pass the string to split. Ultimately, the explode() function returns an array in which each element is a comma-separated portion of the original string followed by a space.
3. Use the preg_split() function to convert the string into an array
If you need to use a regular expression to specify the separator, you can use the preg_split() function. This function is similar to the split() and explode() functions, but it uses regular expressions to match delimiters. The following is a sample code:
<?php //创建一个字符串 $str = "1-alpha, 2-beta, 3-gamma"; //用preg_split()函数将字符串转换成数组 $arr = preg_split("/-/", $str); //输出结果 print_r($arr); ?>
In the above code, we use the preg_split() function to convert a string containing three numbers and dashes into an array. In this function, the first parameter is the regular expression pattern, in this example, we use dash as delimiter. Then in the second parameter we pass the string to split. Finally, the preg_split() function returns an array in which each element is a dash-split portion of the original string.
Summary
In this article, we learned how to use PHP built-in functions to convert a string into an array. During the development process, these functions can help us process and manage large amounts of data and simplify the code implementation process. Although the methods used by these functions are different, they all can easily convert a string into an array. You need to choose the appropriate function to implement according to your own needs.
The above is the detailed content of PHP function example: convert string to array. For more information, please follow other related articles on the PHP Chinese website!