Saturday, September 18, 2010

Using explode in php for beginners

how to explode (split, break apart) string in php using explode function 

<?php

function split($stringtosplit)
    {
    $abc = explode(' ', $stringtosplit);
    foreach($abc as $xyz){
    echo  $stringtosplit;
    }
    return $abc;
    }
   
?>

for example

<?php

    split ('the quick brown fox jumps over the lazy dogs');
    ?>

output : thequickbrownfoxjumpsoverthelazydogs
in this output all the space were removed via explode function, to format this we need to style our string

i.e. echo  $stringtosplit .'<br />';

now output will be

the
quick
brown
fox
jumps
over
the
lazy
dogs
 

No comments: