PHP » Strings » explode()

Syntax:
array explode(string separator, string s [, int limit])
separator
Sequence of characters separating the portions that will make elements.
s
The string that needs divided.
limit
Maximum number of elements returned.

Makes an array of a string.

This function takes a string and a separator and returns an array where each element is a portion of the string separated from other portions by the separator.

Examples

Code:
<?php

$text = "192.168.0.1 gw\n192.168.0.2 fileserver\n192.168.0.3 dev";

$hosts = explode("\n", $text);
foreach ($hosts as $host) {
   print "$host<br>";
}

?>
Output:
192.168.0.1 gw
192.168.0.2 fileserver
192.168.0.3 dev
Explanation:

A string contains a few lines of data that could be from a "hosts" file. The explode() function is used to get an array with one element for each host.

See Also: