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

How to convert string to array using comma in php

藏色散人
藏色散人Original
2020-08-24 09:12:383155browse

php method to use commas to convert a string into an array: first create a PHP sample file; then use the "explode(",",$str)" method to break up a string into an array .

How to convert string to array using comma in php

Recommended: "PHP Video Tutorial"

Strings are broken into arrays

<?php
$str = "Hello world. I love php!";
print_r (explode(" ",$str));
?>

Use explode to break up a string into an array. However, explode uses a certain character to break it up. For example, in the above example, the first parameter in the parentheses of explode is a space, and the second parameter is a space. is the string we are going to scramble, so concatenation means breaking the $str string into characters through spaces.

Because the content of our string is Hello world. I love php!

Each word is separated by a space, then after we break it up by spaces, our string will be It can be turned into an array.

If our Hello, world, Ilove, php!

are separated by commas, then we modify

explode(“,”,$str) first Just fill in the commas "," for each parameter.

The above is the detailed content of How to convert string to array using comma 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