Monday, April 30, 2012

PHP Explode Function Tutorial


Explode Function
syntax:
$array_name = explode($delimiter, $string);
The explode() function is used to split up strings based on the specified delimiter.
It is similar to the split function in Javascript.
Once the string is split or broken up, the pieces are stored in an array and they are numbered in order, starting from 0.
Example: 1
        <?php
        $str = "I am responsible for my own health.";
        print_r (explode(" ",$str));
        ?>
Another way:
        <?php
        $str = "I am responsible for my own health.";
        $parts = (explode(" ",$str));
        print_r ($parts);
        ?>
Output
Array ( [0] => I [1] => am [2] => responsible [3] => for [4] => my [5] => own [6] => health. ) 

We can apply explode function on date or phone number.
Let's see an example.
Example: 2
        <?php
        $D_M_Y = date("d/m/Y");
        $parts = (explode('/' ,$D_M_Y));
        print_r ($parts);
        ?>
Output
Array ( [0] => 30 [1] => 04 [2] => 2012 ) 

Notice that today's date is broken.

No comments:

Post a Comment