通过换行拆分MySQL转储

时间:2015-10-28 00:39:55

标签: php regex preg-split array-filter

我有一个字符串(来自mysql转储):

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `parent` int(11) DEFAULT NULL,
  `description` text NOT NULL,
  `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `ordering` int(11) NOT NULL DEFAULT '1000',
  `published` tinyint(1) NOT NULL,
  `image` varchar(255) NOT NULL,
  `banner_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `parent` (`parent`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=14 ;

INSERT INTO `categories` (`id`, `name`, `parent`, `description`, `created`, `ordering`, `published`, `image`, `banner_id`) VALUES
(1, 'Anniversary', 0, 'Lorem Ipsum; is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry''s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.', '2015-07-13 00:00:00', 0, 1, 'images/flowers/1.jpg', 2),
(5, 'Get Well', 0, 'Lorem Ipsum; is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry''s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.', '2015-07-13 00:00:00', 5, 1, 'images/flowers/6.jpg', 0);

我想得到并执行数组:

Array(
   [0]=>CREATE TABLE IF NOT EXISTS `categories` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `parent` int(11) DEFAULT NULL,
      `description` text NOT NULL,
      `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
      `ordering` int(11) NOT NULL DEFAULT '1000',
      `published` tinyint(1) NOT NULL,
      `image` varchar(255) NOT NULL,
      `banner_id` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `parent` (`parent`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=14 ;
   [1]=> INSERT INTO `categories` (`id`, `name`, `parent`, `description`, `created`, `ordering`, `published`, `image`, `banner_id`) VALUES
    (1, 'Anniversary', 0, 'Lorem Ipsum; is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry''s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.', '2015-07-13 00:00:00', 0, 1, 'images/flowers/1.jpg', 2),
    (5, 'Get Well', 0, 'Lorem Ipsum; is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry''s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.', '2015-07-13 00:00:00', 5, 1, 'images/flowers/6.jpg', 0);
)

我试过用:

$sql_array = array_filter(explode(';', trim(preg_replace('/\s\s+/', ' ', $extension->sql))));

但我错了,因为我不能分开 - ; 谢谢!

1 个答案:

答案 0 :(得分:0)

我用分号分开然后换行。

SELECT Depts.Department, IF (Employees.Salary>100000, 'Rich', 'Poor'), COUNT(*) FROM `Employees`, `Depts` WHERE Depts.Dept = Employees.Dept GROUP BY Depts.Department, IF (Employees.Salary>100000, 'Rich', 'Poor')

或者可能$sql_array = preg_split('/;\n/', $extension->sql);取决于行结尾