PHP explode and implode for manipulate string or array
From w3cyberlearnings
Contents |
What is explode in php?
- Splitting a string according to the string delimiter, and return the array of strings.
explode method syntax
array explode ( string $delimiter , string $string )
explode example 1
<?php $country ="China Korea Japan Malaysia Thailand Cambodia Vietnam America India"; $countries = explode(" ", $country); echo $countries[0] . "<br/>"; echo $countries[1] . "<br/>"; ?>
Display Result
China Korea
explode example 2
<?php $name ="Jing|Bob Maat|Christina Zhar|Yiling Mang|Soung Ta"; $names = explode("|",$name); echo $names[0] . "<br/>"; echo $names[1] . "<br/>"; ?>
Display result
Jing Bob Maat
What is implode method in PHP?
- Implode is the opposite function of explode. We use implode to put array back to a string.
implode example
<?php $fruits = array("apple","orange","banana","blueberry","mango"); $fruits_str = implode(", ",$fruits); echo $fruits_str . "<br/>"; ?>
Display Result
apple, orange, banana, blueberry, mango