implode and explode function
Implode() Function
The implode function is used to join elements of an array with a string. The implode( ) function returns a string from elements of an array
Syntax :
implode (separator , array)
Example
<?php
$arr = array('m','a','h','t','a','b');
$str = implode("-",$arr);
echo $str;
?>
output :
and you wish to combine it into a string, by putting the separator '-' between each element of the array.
m-a-h-t-a-b
Example
<?php
$arr=array ('I','am','simple','boy!');
echo implode(" ",$arr);
?>
output :- I am simple boy!
implode() Example.
File Name:- index.php
<?php
$arr = array ('I','am','simple','boy!');<br/>
$space_separated = implode(" ", $arr);<br/>
$comma_separated = implode(" , ", $arr);<br/>
$slash_separated = implode(" / ", $arr);<br/>
$dot_separated = implode(" . ", $arr);<br/>
$hyphen_separated = implode(" - ", $arr);<br/>
echo $space_separated;<br/>
echo $comma_separated;<br/>
echo $slash_separated;<br/>
echo $dot_separated;<br/>
echo $hyphen_separated;<br/>
?>
Output :-
Explode() Function.
The explode() function is used to Split a string into array.
$str="AEIOU";
now you want to make each name as an element of an array and access it individually so what you do:
$arr = explode(",", $str);
means : we have made pieces of string $text based on separator ',' and put the resulting array in variable $arr.
So I used print_r ($arr); and the results are the following:
Array([0] => A[1] => E[2] => I[3] => O[4] => U)
which is equal to:$arr = Array ("A","E","I","O","U");
File Name:- index.php
<html>
<body bgcolor="pink">
<h3>Explode Function</h3>
<?php
$str="I am simple boy!";
print_r(explode(" ",$str));
print_r(explode("-",$str));
?>
</body>
</html>
Output :-
Explode and substr function.
File name index1.php
$uu = "http://billicat.com/jealous/index2.php?id=1&appname=jealous";
window.location = "<?php echo $uu;?>?"+Hash;
Explode and substr function.
File name index2.php
$id = $_GET['id'];
$str = $_GET['appname'];
// love?value=mahtab
$arr = explode("?", $str);
$appname = $arr[0];
echo $appname;
// love
echo "<br/>";
$data = $str;
// value=mahtab
$access_token = substr($data, strpos($data, "=")+1);
echo $access_token;
How to split a string when finding whitespace ?
File name index2.php
<?php
$str = "mohammad mahtab alam";
$result = explode(" ",$str,2);
print_r($result);
echo $result[0];
// print_r(explode(' ', $str, 2));
?>
<?php
$str = "foo bar php js";
$arr = explode(" ", $str);
//print all the value which are in the array
foreach($arr as $v){
echo $v;
}
?>
How to get current url and extract sub string
File name index2.php
$purl = 'http'.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $purl; // get the current url of the page. http://itechtuto.com?id=1
$urlstr = substr($purl, strrpos($purl, '?') + 1);
echo $urlstr; // it gives id=1
//echo "<br/>";
$arr = explode("=", $urlstr);
$appname = $arr[0];
echo $appname; // it prints only id
//print all the value which are in the array
foreach($arr as $v){
echo $v; echo "<br/>";
}
How to get the First Word of the Sentence or String.
Ex . "md mahtab alam" // output : md
File name index2.php
<?php
$value = "Test me more";
echo strtok($value, " "); // Test
?>
************* Or ******************
<?php
$myvalue = 'Test me more';
$arr = explode(' ',trim($myvalue));
echo $arr[0]; // will print Test
?>
************* Or ******************
<?php
$value = "Hello world";
$tokens = explode(" ", $value);
echo $tokens[0];
?>
Example
<?php
$today="07/05/2020";
$dmy = explode('/', $today);
$day = $dmy[1];
$month = $dmy[0];
$year = $dmy[2];
echo $day."<br>";
echo $month."<br>";
echo $year;
?>
Example
<?php
$today="07/05/2020";
$dmy = explode('-', $today);
$day = $dmy[1];
$month = $dmy[0];
$year = $dmy[2];
echo $day."<br>";
echo $month."<br>";
echo $year;
?>
Previous
Next