The previous lesson of PHP Lessons Series was about Break and Continue Statements in PHP and in this lesson we will learn about Explode and Implode functions in PHP.
The splitting character in the above explode function is the character whose occurrences in the string will decide how many array elements there will be. If there is only one occurrence of the splitting character in the string, the string will be split into two pieces i.e the part before the splitting character and the part after the splitting character. So, the array returned by the explode function will have two elements. An example of PHP Explode function is given below:
The splitting character in the above code is "," and there are 4 occurrences of "," in the string ($countries) so the number of elements in array will be (4+1). The output of the above code is given below:
The joining character in the above implode function is the character that will appear between the array elements in the joined string. An example of PHP Implode function is given below:
The implode() function in the above code will join the elements of array ($arr) into a string ($joinedstr) with spaces between the array elements. The output of the above code is given below:
Related Posts
How to remove backslashes in php?
How to add backslashes in php?
PHP String Functions
More about PHP String Functions
PHP String Conversion Functions
PHP String Comparison Functions
explode()
PHP Explode function is used to split a string into an array. The basic structure of explode function is given below:
Array = explode(splitting character,string);
The splitting character in the above explode function is the character whose occurrences in the string will decide how many array elements there will be. If there is only one occurrence of the splitting character in the string, the string will be split into two pieces i.e the part before the splitting character and the part after the splitting character. So, the array returned by the explode function will have two elements. An example of PHP Explode function is given below:
$countries = "USA,France,England,Russia,Italy";
$arr = explode(",",$countries);
print_r($arr);
The splitting character in the above code is "," and there are 4 occurrences of "," in the string ($countries) so the number of elements in array will be (4+1). The output of the above code is given below:
Output:
Array ( [0] => USA [1] => France [2] => England [3] => Russia [4] => Italy )implode()
PHP Implode function is used to join the elements of an array into a string. The basic structure of implode function is given below:
String = implode(joining character,array);
The joining character in the above implode function is the character that will appear between the array elements in the joined string. An example of PHP Implode function is given below:
$arr = array('This','is','a','joined','string.');
$joinedstr = implode(" ",$arr);
echo $joinedstr;
The implode() function in the above code will join the elements of array ($arr) into a string ($joinedstr) with spaces between the array elements. The output of the above code is given below:
Output:
This is a joined string.Related Posts
How to remove backslashes in php?
How to add backslashes in php?
PHP String Functions
More about PHP String Functions
PHP String Conversion Functions
PHP String Comparison Functions
No comments:
Post a Comment