Search code examples
phpimplode

PHP textarea Implode and explode with comma seperated


I want to implode a textarea name as comma seperated to store in database. I am not getting a exact solution.

HTML code :

<div class="form-group row">
     <label for="input-1" class="col-sm-4 col-form-label">Textarea</label>
     <div class="col-sm-8"> 
          <textarea name="reg_desc" class="form-control" rows="5"></textarea>
     </div>    
</div>   

PHP Code :

$reg_desc = implode(',', $_POST['reg_desc']);

SQL Query :

$sql="INSERT into `regions` (`reg_desc`) VALUES  ('$reg_desc')";

Exactly if i want to insert a record in textarea like (see the screen shot)

enter image description here

It will store with comma seperated in database.


Solution

  • For your solution, you have to split string with new line then replace with comma when you find new line.

    implode(',', explode("\n", $_POST['reg_desc']))