How to take multiple values in an input type text (text box) separated by a comma in PHP, explode function, implode function
In this tutorial, we will learn about the followings;
- Explode function in PHP
- Implode() Function in PHP
- How to take multiple values in an input type text (text box) separated by a comma in PHP
Explode function in PHP
The explode function is used to split a string into pieces by a specified string i.e. it breaks a string into an array.
The explode function in PHP is used to break a string into smaller text with each break occurring at the similar symbol, commonly called delimiter.
Here in the code below, we are separating the multiple values of the textbox with a comma.
Implode() Function in PHP
The implode function is a function to join the elements of an array with a specific string.
The implode() function joins the strings by taking an array of strings and using a delimiter.
Program of multiple values in an input type text (text box) separated by a comma by using explode function
<html>
<body>
<form method="post">
<table border="5" height="200" width="800" >
<tr><td colspan="2"> <center> show multiple values </center> </td></tr>
<tr><td>Enter values separated by comma</td>
<td><input type="text" name="n" placeholder="1,2,3,4,5" /></td></tr>
<tr><td>result</td>
<?php
if (empty($result)) $result=' result comes here' ;
if(isset($_POST['save']))
{
$result=0;
$n1=$_POST['n'];
echo "</br>";
echo "values separated by comma's are : ".$n1;
echo "</br>";
$n1arr=explode(',', $n1);
echo "________________________";
echo "</br>";
echo "value after spliting comma separated values by variable in array is given below </br>";
for ($i=0; $i <count($n1arr) ; $i++)
{
echo "$n1arr[$i]". " | ";
}
echo "</br>";
echo "________________________";
echo "</br>";
}
?>
<td><output><?php echo $result ?></output></td></tr>
<tr><td colspan="2" width="500" align="center"><input type="submit" name="save" value="calculate"></td></tr>
</body>
</html>