2010-12-16 14 views
0

-CodePHP: SQL Ausgabe regex Problem

<?php 
$sql = file_get_contents('source.sql'); 
$regex = '/CREATE TABLE IF NOT EXISTS|INSERT INTO|ALTER TABLE `[0-9a-zA-Z-_]+`/'; 
$matches = array(); 
preg_match_all($regex, $sql, $matches); 
print_r($matches); 
?> 

SQL

CREATE TABLE IF NOT EXISTS `adminnotification_inbox` (
    `notification_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `severity` tinyint(3) unsigned NOT NULL DEFAULT '0', 
    `date_added` datetime NOT NULL, 
    `title` varchar(255) NOT NULL, 
    `description` text, 
    `url` varchar(255) NOT NULL, 
    `is_read` tinyint(1) unsigned NOT NULL DEFAULT '0', 
    `is_remove` tinyint(1) unsigned NOT NULL DEFAULT '0', 
    PRIMARY KEY (`notification_id`), 
    KEY `IDX_SEVERITY` (`severity`), 
    KEY `IDX_IS_READ` (`is_read`), 
    KEY `IDX_IS_REMOVE` (`is_remove`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; 

CREATE TABLE IF NOT EXISTS `admin_assert` (
    `assert_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `assert_type` varchar(20) NOT NULL DEFAULT '', 
    `assert_data` text, 
    PRIMARY KEY (`assert_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='ACL Asserts' AUTO_INCREMENT=1; 

CREATE TABLE IF NOT EXISTS `admin_role` (
    `role_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `parent_id` int(10) unsigned NOT NULL DEFAULT '0', 
    `tree_level` tinyint(3) unsigned NOT NULL DEFAULT '0', 
    `sort_order` tinyint(3) unsigned NOT NULL DEFAULT '0', 
    `role_type` char(1) NOT NULL DEFAULT '0', 
    `user_id` int(11) unsigned NOT NULL DEFAULT '0', 
    `role_name` varchar(50) NOT NULL DEFAULT '', 
    PRIMARY KEY (`role_id`), 
    KEY `parent_id` (`parent_id`,`sort_order`), 
    KEY `tree_level` (`tree_level`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='ACL Roles' AUTO_INCREMENT=4; 

INSERT INTO `admin_role` (`role_id`, `parent_id`, `tree_level`, `sort_order`, `role_type`, `user_id`, `role_name`) VALUES 
(1, 0, 1, 1, 'G', 0, 'Administrators'), 
(3, 1, 2, 0, 'U', 1, 'Template'); 

Ich versuche, warum über den Code zu verstehen, zeigt keine Tabellen-Namen in der Ausgabe, nur Aussagen einfügen und erstellen. ..

Ausgabe

Array ([0] => Array ([0] => CREATE TABLE IF NOT EXISTS [1] => CREA TE TABELLE WENN NICHT EXISTIERT [2] => TABELLE ERSTELLEN WENN NICHT EXISTIERT [3] => INSERT INTO) [1] => Array ([0] => [1] => [2] => [3] =>))

Antwort

1

Es ist, weil der [0-9a-zA-Z-_]+ Teil nur im dritten Teil Ihres "oder" ist (und Sie haben keine ALTER TABLE Aussagen). Verwenden Sie

+0

Sorry, ich verstehe es nicht .... Ich habe versucht, zu verstehen, aber ... Danke – foomatic

+0

Nun, Sie versucht, alle Instanzen von CREATE TABLE übereinstimmen IF NOT EXISTS', 'INSERT INTO' und' ALTER TABLE', gefolgt vom Tabellenname regexp am Ende. Die übereinstimmende Engine weiß jedoch nicht, dass der Tabellenname regexp mit jeder der drei Möglichkeiten übereinstimmen soll, es sei denn, Sie setzen '()' um den ersten Teil. –

+1

Lassen Sie es mich so ausdrücken: Ihre aktuelle Regexp wird wie folgt interpretiert: '(CREATE TABLE WENN NICHT EXISTIERT) | (INSERT INTO) | (ALTER TABLE \' [0-9a-zA-Z -_] + \ ') ' –

1

Wie Spiny sagte, der Tabellenname Übereinstimmung ist Teil der dritten oder Abschnitt. So teilen Sie es heraus könnten Sie:

$regex = '/(CREATE TABLE IF NOT EXISTS|INSERT INTO|ALTER TABLE) (`[0-9a-zA-Z-_]+`)/';