Is their a JavaScript Equivalent to PHP explode()?

The JavaScript equivalent to PHP's explode() function is split(). Both functions break a string into an array based on a specified delimiter.

Syntax

string.split(separator, limit)

Parameters

  • separator: The character or string to split by (required)
  • limit: Maximum number of splits (optional)

Basic Example

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript split() Example</title>
</head>
<body>
    <script>
        var str = '087000764008:Rank:info:result';
        var arr = str.split(":");
        
        console.log("Original string:", str);
        console.log("Split array:", arr);
        console.log("After first colon:", arr[1] + ":" + arr[2]);
    </script>
</body>
</html>
Original string: 087000764008:Rank:info:result
Split array: ['087000764008', 'Rank', 'info', 'result']
After first colon: Rank:info

Different Separators

<!DOCTYPE html>
<html>
<head>
    <title>Different Separators</title>
</head>
<body>
    <script>
        // Split by comma
        var csvData = "apple,banana,orange";
        console.log("CSV split:", csvData.split(","));
        
        // Split by space
        var sentence = "Hello world JavaScript";
        console.log("Word split:", sentence.split(" "));
        
        // Split by multiple characters
        var data = "one::two::three";
        console.log("Double colon split:", data.split("::"));
    </script>
</body>
</html>
CSV split: ['apple', 'banana', 'orange']
Word split: ['Hello', 'world', 'JavaScript']
Double colon split: ['one', 'two', 'three']

Using the Limit Parameter

<!DOCTYPE html>
<html>
<head>
    <title>Split with Limit</title>
</head>
<body>
    <script>
        var str = "one:two:three:four:five";
        
        console.log("No limit:", str.split(":"));
        console.log("Limit 3:", str.split(":", 3));
        console.log("Limit 2:", str.split(":", 2));
    </script>
</body>
</html>
No limit: ['one', 'two', 'three', 'four', 'five']
Limit 3: ['one', 'two', 'three']
Limit 2: ['one', 'two']

Comparison: PHP vs JavaScript

Language Function Syntax
PHP explode() explode(delimiter, string, limit)
JavaScript split() string.split(delimiter, limit)

Conclusion

JavaScript's split() method works exactly like PHP's explode() function, converting strings into arrays using a delimiter. The main difference is that split() is a string method, while explode() takes the string as a parameter.

Updated on: 2026-03-15T23:18:59+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements