Search code examples
phpmysqlseparator

PHP-Converting Comma separated strings into Dot or Symbol separated values for storing in MySql


My current project consist of a MySQL table having 22 fields already.

I want to add a new field namely Permanent address. In the front-end, it's a textarea in which the user will fill the following data with either newlines or comma-separated as per the standard of here. First string will be House name, then Post office, then Place, then Pincode.

So I want to store it in a single column namely "permanent_address". So how do I store this comma/newline separated value into the column in the table?

Can it achieved by converting the comma/newline values into symbol ( @,#,%,&, ) separated values*, so that it can be stored to DB without violating 1st NF

enter image description here


Solution

  • You can use explode function to split the text into different parts. It will work like this,

    $parts = explode(",",$input);  // this will split input in array, $input will have the text input
    $street_name = $parts[0];      // gives the first part 
    

    If you want to split by both "," and new line "\n", you can use,

    $parts = preg_split( "/[,|\n]/", $input );