次のようにmysqlテーブルの列を解析する方法 <14-abc>;<8-def>;
:
abc =14
def = 8
abcは値14を格納し、defは値8を格納し、複数のエントリを持ちます。
したがって、後で解析されたデータにアクセスして処理できるようにする必要がある配列またはリストのように格納する必要があります。
私はこの構文をサポートするパーサーを知らないので、非常に些細なことのために独自のパーサーを書くことができます。テストされていない例を次に示します。
$row = ' <14-abc>;<8-def>; ';
$output = Array();
$entries = explode(';', $row); //Split the row by every ';' delimiter
foreach ($entries as $item) { //For each entries
if (!empty($item)) { //Make sure the item isn't empty (last entry might be)
$item = str_replace(' ','',$item); //Remove useless char
$item = str_replace('<','',$item); //Remove useless char
$item = str_replace('>','',$item); //Remove useless char
$params = explode('-', $item); //Again, subsplit the key and the value from the item
$output[$params[1]] = $params[0]; //Push them into an array
}
}
次に、次のように素晴らしい配列を使用できます。
foreach ($output as $key=>$value) {
echo $key.'='.$value;
}
入力は$row
文字列で、出力は$output
配列です。関数にラップすることもできます;)
?php
function parser($row)
{
$output = Array();
$entries = explode(';', $row); //Split the row by every ';' delimiter
foreach ($entries as $item) { //For each entries
if (!empty($item)) { //Make sure the item isn't empty (last entry might be)
$item = str_replace(' ','',$item); //Remove extra char
$item = str_replace('<','',$item); //Remove extra char
$item = str_replace('>','',$item); //Remove extra char
$params = explode('-', $item); //Again, subsplit the key and the value from the item
$output[$params[1]] = $params[0]; //Push them into an array
}
}
foreach ($output as $key=>$value) {
$sql1 = "INSERT INTO `tablename`(`column name`, `column name`) VALUES ('$key','$value')";
mysql_query($sql1);
}
}
?>
これですべての作業が行われます。パーサー関数を定義し、データを解析して、結果をDBに挿入します。