如何拆分长字符串并使用php插入mysql数据库?

时间:2016-12-22 17:41:24

标签: php mysql sql split

我有一个包含以下字符串的长字符串: -

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

以下是我要分割并插入上述6列的字符串: -

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

如何拆分此字符串并插入其各自的列? 任何帮助或建议表示赞赏。谢谢。

2 个答案:

答案 0 :(得分:2)

您可以使用以下代码

<?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);
?>

答案 1 :(得分:0)

您可以使用正则表达式执行此操作。只要您的数据始终具有上述格式,以下代码就会将您的数据拆分为具有相应键名的数组数组:

food - ingredients

输出:

<?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);

这是一个calls the callback function。这使用了demoArray ( [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 ) ) 语法)的魔力。