Tuesday 7 February 2012

How 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.


Code:
    $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>";


No comments:

Post a Comment