How to use Implode And Explode Function In Php

 How to use Implode And Explode Function In Php

Implode function- The implode() function returns a string from the elements of an array.

Syntax-

implode (separator , array)

example1-

<?php

$line=array ('I','am','a','student');

echo implode(" ",$line);

?>

Result-I am a student



Example2-

<?php

$arr = array ('I','am','a','boy');

echo implode(" ", $arr);



echo '<br>';

echo  implode(" , ", $arr);

echo '<br>';



echo implode(" / ", $arr);

echo '<br>';

echo  implode(" . ", $arr);

echo '<br>';

echo implode(" - ", $arr);



?>





Result-I , am , a , boy!

I / am / a / boy!

I . am . a . boy!

I - am - a - boy!




How To Use Explode Function-The explode() function breaks a string into an array.

Syntax-explode (separator,string,limit)
Example1-<html>

<body>

<?php

$str="I am simple boy!";

print_r(explode(" ",$str,4));

?>

</body>

</html>

Result-Array ( [0] => I [1] => am [2] => simple [3] => boy! )


Example2-

<?php

$str="I am simple boy!";

print_r(explode("-",$str));

?>


Result- Array ( [0] => I am simple boy! )




Previous
Next Post »
Thanks for your comment