Home  >  Article  >  Backend Development  >  How to convert string to array in php?

How to convert string to array in php?

青灯夜游
青灯夜游Original
2020-04-25 17:06:407510browse

How to convert string into array in php? The following article will introduce to you how to convert a string into an array in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to convert string to array in php?

In PHP, converting a string into an array is a very frequently used operation. It is also usually called breaking the string into an array. You can use its built-in function explode().

explode() function breaks up the string into an array.

Note: The "separator" parameter cannot be an empty string.

Note: This function is binary safe.

Specific method introduction:

1. First create a new PHP document and define a string, example:

$str = 'apple|banana|orange|carrot';

How to convert string to array in php?

2. Use "|" as the delimiter to break the string into an array. Example:

$arr = explode('|',$str);

How to convert string to array in php?

3. Use print_r to convert the string. Print out the array, example:

print_r($arr);

How to convert string to array in php?

4. Save the above content and view the array after string conversion in the browser,

How to convert string to array in php?

5. The explode() function also has a third parameter, which can limit the number of array elements. Example:

$arr = explode('|',$str,2);
print_r($arr);

How to convert string to array in php?

6. Save the file again and preview it. Printing after limiting the number of elements

How to convert string to array in php?

Note

The delimiter of the explode() function is required and can be customized For more related knowledge of

, please pay attention to PHP Chinese website! !

The above is the detailed content of How to convert string to array in php?. For more information, please follow other related articles on the PHP Chinese website!

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