Silahkan Melihat Tutorial di website kami dengan nyaman ENJOY YOUR LIFE ☕

Implode And Explode Function In Php

Hello All,

Here I Want give example of how to use implode and explode in php.

Use of Implode 


=> The implode() function returns a string from the elements of an array. separator parameter of implode() is optional.

I give example for it.


$array = array("hello","all","be","happy","always");

$result =  implode($array);

echo $result;

it result will be => helloallbehappyalways.

if you can separate array using space then you can write this way

 $result =  implode(" ",$array);

this result will me => hello all be happy always

you can any of parameter to separate array;

like i give emaple for it


echo $result =  implode("$",$array)."<br/>";
echo $result =  implode("+",$array)."<br/>";
echo $result =  implode("-",$array)."<br/>";
echo  $result =  implode("*",$array)."<br/>";

this result will me =>

hello$all$be$happy$always
hello+all+be+happy+always
hello-all-be-happy-always
hello*all*be*happy*always

I think yon can understand what implode work it can make string of array using separate
by it parameter.you can use different parameter that can separate it. 

Use of Explode

=> this function use of breaks a string into an array.separator parameter cannot be empty string.

i will give example for it.

$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry";
$result = explode(" ",$string);
print_r($result);

this result will be=>
Array ( [0] => Lorem [1] => Ipsum [2] => is [3] => simply [4] => dummy [5] => text [6] => of [7] => the [8] => printing [9] => and [10] => typesetting [11] => industry )

if you want to separate to this different word.

if you to get separate word then check below code

echo "<br/>";
 $count = count($result);
 
 for($i=1;$i<=$count;$i++){
    
    $line =  each($result);
    print_r($line);
    echo "<br/>";
    echo $line['value']."<br/>";
 }

this out put will be
=>
Lorem
Array ( [1] => Ipsum [value] => Ipsum [0] => 1 [key] => 1 )
Ipsum
Array ( [1] => is [value] => is [0] => 2 [key] => 2 )
is
Array ( [1] => simply [value] => simply [0] => 3 [key] => 3 )
simply
Array ( [1] => dummy [value] => dummy [0] => 4 [key] => 4 )
dummy
Array ( [1] => text [value] => text [0] => 5 [key] => 5 )
text
Array ( [1] => of [value] => of [0] => 6 [key] => 6 )
of
Array ( [1] => the [value] => the [0] => 7 [key] => 7 )
the
Array ( [1] => printing [value] => printing [0] => 8 [key] => 8 )
printing
Array ( [1] => and [value] => and [0] => 9 [key] => 9 )
and
Array ( [1] => typesetting [value] => typesetting [0] => 10 [key] => 10 )
typesetting
Array ( [1] => industry [value] => industry [0] => 11 [key] => 11 )
industry

if you want to get only word echo $line['value]; use it and print_r($line); comment it.

you can also limit parameter to return a number of array elements

check this example

$string = "Lorem,Ipsum,is,simply,dummy,text,of,the,printing,and,typesetting,industry";

if you want to only 1 key then code this

print_r(explode(',',$string,0));

this result will =>
Array ( [0] => Lorem,Ipsum,is,simply,dummy,text,of,the,printing,and,typesetting,industry )

if you want 2 key then

print_r(explode(',',$string,2));

this result will=>
Array ( [0] => Lorem [1] => Ipsum,is,simply,dummy,text,of,the,printing,and,typesetting,industry )

if you want all key then you can use this code

print_r(explode(',',$string,-1));


this result will be =>

Array ( [0] => Lorem [1] => Ipsum [2] => is [3] => simply [4] => dummy [5] => text [6] => of [7] => the [8] => printing [9] => and [10] => typesetting )

this use of explode and implode. explode use for string to array and implode use for array to string.

 



 





0 komentar:

Post a Comment

Implode And Explode Function In Php