AndroidNewBee
AndroidNewBee

Reputation: 744

How can I split a long string and insert into a mysql databse using php?

I have a long string containing the following strings :-

1)serviceName
2)categoryName
3)itemName
4)itemRate
5)itemQuantity
6)itemTotal

Following is the string I want to split and insert into the above 6 columns:-

Laundry, men, shirt, 60, 3, 180, Laundry, men, T-shirt, 50, 2, 100, Laundry, men, jeans/Trousers, 60, 2, 120, Laundry, women, blouse, 50, 2, 100, Laundry, women, dress, 120, 2, 240, Laundry, women, long dress, 190, 2, 380, Laundry, women, saree, 160, 1, 160, Laundry, household, bedspread single, 90, 2, 180, Laundry, household, bedspread double, 140, 2, 280, Laundry, household, bedsheet single, 60, 2, 120, Laundry, household, bedsheet double, 90, 2, 180

How can I split this string and insert into it's respective columns? Any help or suggestion is appreciated.Thank you.

Upvotes: 0

Views: 65

Answers (2)

elixenide
elixenide

Reputation: 44823

You can do this with a regex. As long as your data always has the above format, the following code will split your data into an array of arrays, with appropriate key names:

<?php

$data = "Laundry, men, shirt, 60, 3, 180, Laundry, men, T-shirt, 50, 2, 100, Laundry, men, jeans/Trousers, 60, 2, 120, Laundry, women, blouse, 50, 2, 100, Laundry, women, dress, 120, 2, 240, Laundry, women, long dress, 190, 2, 380, Laundry, women, saree, 160, 1, 160, Laundry, household, bedspread single, 90, 2, 180, Laundry, household, bedspread double, 140, 2, 280, Laundry, household, bedsheet single, 60, 2, 120, Laundry, household, bedsheet double, 90, 2, 180";

$regex = '/(?P<serviceName>[^,]+),\s*(?P<categoryName>[^,]+),\s*(?P<itemName>[^,]+),\s*(?P<itemRate>[^,]+),\s*(?P<itemQuantity>[^,]+),\s*(?P<itemTotal>[^,]+)(?:,\s*|$)/';

preg_match_all($regex, $data, $matches, PREG_SET_ORDER);

print_r($matches);

Output:

Array
(
    [0] => Array
        (
            [0] => Laundry, men, shirt, 60, 3, 180, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => men
            [2] => men
            [itemName] => shirt
            [3] => shirt
            [itemRate] => 60
            [4] => 60
            [itemQuantity] => 3
            [5] => 3
            [itemTotal] => 180
            [6] => 180
        )

    [1] => Array
        (
            [0] => Laundry, men, T-shirt, 50, 2, 100, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => men
            [2] => men
            [itemName] => T-shirt
            [3] => T-shirt
            [itemRate] => 50
            [4] => 50
            [itemQuantity] => 2
            [5] => 2
            [itemTotal] => 100
            [6] => 100
        )

    [2] => Array
        (
            [0] => Laundry, men, jeans/Trousers, 60, 2, 120, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => men
            [2] => men
            [itemName] => jeans/Trousers
            [3] => jeans/Trousers
            [itemRate] => 60
            [4] => 60
            [itemQuantity] => 2
            [5] => 2
            [itemTotal] => 120
            [6] => 120
        )

    [3] => Array
        (
            [0] => Laundry, women, blouse, 50, 2, 100, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => women
            [2] => women
            [itemName] => blouse
            [3] => blouse
            [itemRate] => 50
            [4] => 50
            [itemQuantity] => 2
            [5] => 2
            [itemTotal] => 100
            [6] => 100
        )

    [4] => Array
        (
            [0] => Laundry, women, dress, 120, 2, 240, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => women
            [2] => women
            [itemName] => dress
            [3] => dress
            [itemRate] => 120
            [4] => 120
            [itemQuantity] => 2
            [5] => 2
            [itemTotal] => 240
            [6] => 240
        )

    [5] => Array
        (
            [0] => Laundry, women, long dress, 190, 2, 380, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => women
            [2] => women
            [itemName] => long dress
            [3] => long dress
            [itemRate] => 190
            [4] => 190
            [itemQuantity] => 2
            [5] => 2
            [itemTotal] => 380
            [6] => 380
        )

    [6] => Array
        (
            [0] => Laundry, women, saree, 160, 1, 160, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => women
            [2] => women
            [itemName] => saree
            [3] => saree
            [itemRate] => 160
            [4] => 160
            [itemQuantity] => 1
            [5] => 1
            [itemTotal] => 160
            [6] => 160
        )

    [7] => Array
        (
            [0] => Laundry, household, bedspread single, 90, 2, 180, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => household
            [2] => household
            [itemName] => bedspread single
            [3] => bedspread single
            [itemRate] => 90
            [4] => 90
            [itemQuantity] => 2
            [5] => 2
            [itemTotal] => 180
            [6] => 180
        )

    [8] => Array
        (
            [0] => Laundry, household, bedspread double, 140, 2, 280, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => household
            [2] => household
            [itemName] => bedspread double
            [3] => bedspread double
            [itemRate] => 140
            [4] => 140
            [itemQuantity] => 2
            [5] => 2
            [itemTotal] => 280
            [6] => 280
        )

    [9] => Array
        (
            [0] => Laundry, household, bedsheet single, 60, 2, 120, 
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => household
            [2] => household
            [itemName] => bedsheet single
            [3] => bedsheet single
            [itemRate] => 60
            [4] => 60
            [itemQuantity] => 2
            [5] => 2
            [itemTotal] => 120
            [6] => 120
        )

    [10] => Array
        (
            [0] => Laundry, household, bedsheet double, 90, 2, 180
            [serviceName] => Laundry
            [1] => Laundry
            [categoryName] => household
            [2] => household
            [itemName] => bedsheet double
            [3] => bedsheet double
            [itemRate] => 90
            [4] => 90
            [itemQuantity] => 2
            [5] => 2
            [itemTotal] => 180
            [6] => 180
        )

)

Here's a demo. This uses the magic of named subpatterns (the (?P<foo>...) syntax).

Upvotes: 0

Abhishek
Abhishek

Reputation: 1068

You can use following code

<?php
$str = "Laundry, men, shirt, 60, 3, 180, Laundry, men, T-shirt, 50, 2, 100, Laundry, men, jeans/Trousers, 60, 2, 120, Laundry, women, blouse, 50, 2, 100, Laundry, women, dress, 120, 2, 240, Laundry, women, long dress, 190, 2, 380, Laundry, women, saree, 160, 1, 160, Laundry, household, bedspread single, 90, 2, 180, Laundry, household, bedspread double, 140, 2, 280, Laundry, household, bedsheet single, 60, 2, 120, Laundry, household, bedsheet double, 90, 2, 180";
$array = explode(",",$str);
$newarray = array();
$temp = "";
for($i=0;$i<sizeof($array);$i++)
{
     if(($i+1)%6==0)
     {
           $temp .= $array[$i];
           array_push($newarray,$temp);
           $temp = "";
     }else{
           $temp .= $array[$i].","; 
     }

}
print_r($newarray);
?>

Upvotes: 2

Related Questions