How to make an array from string using php?

Just a week before we have got a requirement, to make an array from string using php.

To satisfy our requirement we have used php explode function.

The explode() is breaking a string into an array, and returns an array of strings.

It has three arguments which are separator, string and limit.

Where first and second arguments are mandatory and third is optional.

First argument is separator, which describes where to break the string.

Second argument is string to break.

And the third argument is limit, which describes the maximum number of elements an array will contain.

Follow the under given example, which makes an array after breaking a string.

$string = "Hello World! This will make an array from string.";
echo "<pre>";
print_r(explode(" ", $string));
print_r(explode(" ", $string, 2));
print_r(explode(" ", $string, -2));
print_r(explode(" ", $string, 0));
echo "</pre>";

Speak Your Mind

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: