Home > Backend Development > PHP Tutorial > How Can I Explode a Comma-Delimited String into an Array in PHP?

How Can I Explode a Comma-Delimited String into an Array in PHP?

DDD
Release: 2024-12-25 05:48:20
Original
695 people have browsed it

How Can I Explode a Comma-Delimited String into an Array in PHP?

Exploding a Comma-Delimited String into an Array

When working with comma-separated strings, the need to split them into individual array elements often arises.

Problem: Convert a comma-delimited string into a flat, indexed array.

Solution:

PHP provides the explode() function specifically designed for this task. It takes two arguments: a delimiter (in this case, the comma ,) and the string to be exploded.

$inputString = "9,[email protected],8";
$myArray = explode(",", $inputString);

print_r($myArray);
Copy after login

Output:

Array
(
    [0] => 9
    [1] => [email protected]
    [2] => 8
)
Copy after login

Note:

  • If the input string contains multiple consecutive commas, an empty array element will be created for each extra comma.
  • To handle potential security issues when working with user-provided input, be sure to properly filter and sanitize the string before exploding it.

The above is the detailed content of How Can I Explode a Comma-Delimited String into an Array in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template