PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP explode(): Convert String To Array

The explode() function in PHP is a built-in function that is used to split a string by a specified string into an array. It's particularly useful when you want to break down a string into smaller parts.

Here's the basic syntax for explode():

explode(separator, string, limit);

Parameters:

  • separator: Required. Specifies where to break the string. If this is an empty string, explode() will return false.
  • string: Required. The string to split.
  • limit: Optional. Specifies the maximum number of array elements to return. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

Let's see some examples:

  • Basic usage of explode():
$str = "Hello, how are you?";
$arr = explode(" ", $str);
print_r($arr);

Output:

Array
(
    [0] => Hello,
    [1] => how
    [2] => are
    [3] => you?
)

In this example, the explode() function splits the string $str whenever it encounters a space (" "), and it returns an array of the words in the string.

  • Using explode() with a limit parameter:
$str = "Hello, how are you?";
$arr = explode(" ", $str, 2);
print_r($arr);

Output:

Array
(
    [0] => Hello,
    [1] => how are you?
)

In this example, the limit parameter is set to 2, so the explode() function returns an array with a maximum of 2 elements. The first element is the first word, and the second element is the rest of the string.

  • Using explode() with different separators:
$str = "one,two,three,four";
$arr = explode(",", $str);
print_r($arr);

Output:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)

In this example, we're using a comma (",") as the separator, so the explode() function splits the string wherever it encounters a comma.

The explode() function is a powerful tool for string manipulation in PHP. It's particularly useful when working with data that's delivered as a string, but you need to process it as individual items, such as CSV data or space-separated values.

  1. How to Use explode() in PHP for String to Array Conversion:

    The explode() function is used to split a string into an array based on a specified delimiter.

    <?php
    $string = "apple,banana,orange";
    $fruits = explode(",", $string);
    
    print_r($fruits);
    // Output: Array ( [0] => apple [1] => banana [2] => orange )
    ?>
    
  2. PHP explode() with Delimiter and String Parameters:

    You can specify both the delimiter and the input string parameters for explode().

    <?php
    $string = "John|Doe|30";
    $userInfo = explode("|", $string);
    
    print_r($userInfo);
    // Output: Array ( [0] => John [1] => Doe [2] => 30 )
    ?>
    
  3. Exploding Strings into Arrays using PHP explode():

    The explode() function is commonly used to split strings into arrays, such as when parsing user input.

    <?php
    $input = "one two three";
    $words = explode(" ", $input);
    
    print_r($words);
    // Output: Array ( [0] => one [1] => two [2] => three )
    ?>
    
  4. Handling Multiple Delimiters with explode() in PHP:

    You can use explode() with multiple delimiters by first replacing them with a single delimiter using str_replace().

    <?php
    $string = "apple,banana;orange";
    $string = str_replace([";", ","], "|", $string);
    $fruits = explode("|", $string);
    
    print_r($fruits);
    // Output: Array ( [0] => apple [1] => banana [2] => orange )
    ?>
    
  5. PHP explode() vs preg_split() for String Splitting:

    While explode() is simpler, preg_split() allows for more complex patterns.

    <?php
    $string = "apple,banana;orange";
    $fruits = explode(",", $string); // Using explode
    // OR
    $fruits = preg_split("/[,;]/", $string);
    
    print_r($fruits);
    // Output: Array ( [0] => apple [1] => banana [2] => orange )
    ?>
    
  6. Extracting Values from CSV Strings with explode() in PHP:

    explode() is commonly used to extract values from comma-separated values (CSV) strings.

    <?php
    $csvString = "John,Doe,30";
    $userInfo = explode(",", $csvString);
    
    print_r($userInfo);
    // Output: Array ( [0] => John [1] => Doe [2] => 30 )
    ?>
    
  7. Removing Empty Array Elements with explode() in PHP:

    You can use array_filter() to remove empty elements after using explode().

    <?php
    $string = "apple,,banana,,orange";
    $fruits = array_filter(explode(",", $string));
    
    print_r($fruits);
    // Output: Array ( [0] => apple [2] => banana [4] => orange )
    ?>
    
  8. Exploding Strings and Limiting the Number of Elements in PHP:

    Limit the number of elements returned by using the optional third parameter.

    <?php
    $string = "apple,banana,orange,grape";
    $fruits = explode(",", $string, 2);
    
    print_r($fruits);
    // Output: Array ( [0] => apple [1] => banana,orange,grape )
    ?>
    
  9. PHP implode() and explode() for Bidirectional Conversion:

    Use implode() to join array elements back into a string.

    <?php
    $fruits = ["apple", "banana", "orange"];
    $string = implode(",", $fruits);
    
    echo $string;
    // Output: apple,banana,orange
    ?>
    
  10. Using explode() to Parse Query Strings in PHP:

    Parse query strings into associative arrays for easy access to parameters.

    <?php
    $queryString = "name=John&age=30&city=NewYork";
    parse_str($queryString, $params);
    
    print_r($params);
    // Output: Array ( [name] => John [age] => 30 [city] => NewYork )
    ?>
    
  11. PHP explode() and array_map() for Custom Processing:

    Combine explode() with array_map() for custom processing of array elements.

    <?php
    $csvString = "John,Doe,30";
    $userInfo = array_map('trim', explode(",", $csvString));
    
    print_r($userInfo);
    // Output: Array ( [0] => John [1] => Doe [2] => 30 )
    ?>
    
  12. PHP explode() for Splitting Strings with Spaces:

    explode() can be used to split strings based on spaces.

    <?php
    $sentence = "This is a sample sentence";
    $words = explode(" ", $sentence);
    
    print_r($words);
    // Output: Array ( [0] => This [1] => is [2] => a [3] => sample [4] => sentence )
    ?>