2014-04-30 22 views
0

我檢查了很多,但我無法得到它的一個竅門。拆分字符串by;

我需要將SQL轉儲分解爲查詢。

什麼,我需要的基本屬於採取字符串是這樣的:

DROP TABLE IF EXISTS `GDN_Activity`; 

CREATE TABLE `GDN_Activity` (
    `ActivityID` int(11) NOT NULL AUTO_INCREMENT, 
    `ActivityTypeID` int(11) NOT NULL, 
    `NotifyUserID` int(11) NOT NULL DEFAULT '0', 
    `ActivityUserID` int(11) DEFAULT NULL, 
    `RegardingUserID` int(11) DEFAULT NULL, 
    `Photo` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `HeadlineFormat` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `Story` text COLLATE utf8_unicode_ci, 
    `Format` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `Route` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `RecordType` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `RecordID` int(11) DEFAULT NULL, 
    `InsertUserID` int(11) DEFAULT NULL, 
    `DateInserted` datetime NOT NULL, 
    `InsertIPAddress` varchar(39) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `DateUpdated` datetime NOT NULL, 
    `Notified` tinyint(4) NOT NULL DEFAULT '0', 
    `Emailed` tinyint(4) NOT NULL DEFAULT '0', 
    `Data` text COLLATE utf8_unicode_ci, 
    PRIMARY KEY (`ActivityID`), 
    KEY `IX_Activity_Notify` (`NotifyUserID`,`Notified`), 
    KEY `IX_Activity_Recent` (`NotifyUserID`,`DateUpdated`), 
    KEY `IX_Activity_Feed` (`NotifyUserID`,`ActivityUserID`,`DateUpdated`), 
    KEY `IX_Activity_DateUpdated` (`DateUpdated`), 
    KEY `FK_Activity_InsertUserID` (`InsertUserID`) 
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 



LOCK TABLES `GDN_Activity` WRITE; 
INSERT INTO `GDN_Activity` VALUES (1,17,-1,5,NULL,NULL,'{ActivityUserID,You} joined.','Welcome Aboard!',NULL,NULL,NULL,NULL,NULL, 
'2014-04-30 08:53:43','127.0.0.1','2014-04-30 08:53:49',0,0,'a:1:{s:15:\"ActivityUserIDs\";a:2:{i:0;s:1:\"2\";i:1;s:1:\"1\";}}'),(2,15,-1,2,4,NULL, 
'{RegardingUserID,you} → {ActivityUserID,you}', 
'Ping! An activity post is a public way to talk at someone. When you update your status here, it posts it on your activity feed.','Html', 
NULL,NULL,NULL,4,'2014-04-30 08:53:49',NULL,'2014-04-30 08:53:49',0,0,NULL),(3,15,-1,5,3,NULL, 
'{RegardingUserID,you} → {ActivityUserID,you}', 
'Ping! An activity post is a public way to talk at someone. When you update your status here, it posts it on your activity feed.','Html', 
NULL,NULL,NULL,3,'2014-04-30 08:53:52',NULL,'2014-04-30 08:53:52',0,0,NULL); 
UNLOCK TABLES;:-) 

什麼,我需要做的就是分別讓每一個查詢。問題是,如果我將文件拆分爲;,它還會拆分包含;的字符串,而不僅僅是以;結尾的那些字符串。

喜歡這個部分:a:1:{s:15:\"ActivityUserIDs\";a:2:{i:0;s:1:\"2\";i:1;s:1:\"1\";}}。我不希望這種分裂。

我試過了preg_match()preg_split(),但是我無法得到想要的結果。

我試過這種模式:/[\;]+/和其他多個模式,但我不能得到它的工作。

我也試過替換;在那裏沒有''與**,然後爆炸它,但仍然沒有結果。

謝謝。

+2

你只感興趣的';'前述換行符。由此分割? (除了最後一行中的笑臉) – ZeissS

+0

https://www.google.de/search?q=php+sql+parser – GhostGambler

+0

爲什麼不嘗試分割爲'),' –

回答

5

你所尋找的是一個SQL分析器

你可能想看看here

+1

是的!使用正則表達式這個東西是脆弱的地獄。即使它「有效」。 – monocell

+0

向項目中添加一個新班級將不起作用,因爲我的客戶僅僅使用了代碼 – Indra

+0

@Indra,您可能想要學習答案中指定的班級,並提出解決方案,然後 – Guns

1

;\n分割,而不是僅分割;

這確實要求每個查詢都在它自己的行(或多行)上。您仍然可能會遇到一些異常情況,但這是一個99%的解決方案。如果你想確定,使用解析器。 SQL不是上下文無關的,所以理論上正則表達式不能解析它。

$s = "test; 
test2; 
test3;a; 
test4 
;"; 

preg_match_all("/(.*?);(\r?\n|$)/s", $s, $matches); 
var_dump($matches[1]); 

給出:

array(4) { 
    [0]=> string(4) "test" 
    [1]=> string(5) "test2" 
    [2]=> string(7) "test3;a" 
    [3]=> string(7) "test4 " 
} 

我還測試了您的輸入,它似乎很好地工作。

+0

謝謝。它適用於小文件,但我有一個非常大的數據庫。所以這只是一個部分的解決方案。 – Indra