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
DDDOriginal
2024-12-25 05:48:20698browse

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);

Output:

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

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!

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
Previous article:Continuous SubarraysNext article:Continuous Subarrays