preg_match_all没有按预期工作

时间:2014-07-17 14:08:28

标签: php regex preg-match preg-match-all

我有这个问题:

SELECT
    `p`.*,
    `a`.`name`,
    (
        SELECT
            COUNT(`id`)
        FROM
            `accounts`
        WHERE
            `id` = 1
    ) AS `counter`
FROM
    `participants` `p`
LEFT JOIN
    `accounts` `a`
ON
    `p`.`id` = `a`.`id`
WHERE
    `p`.`id` = :id

我使用这个preg_match_all:

preg_match_all("/^(SELECT)(.+?)(FROM)/ims", $sql, $select, PREG_PATTERN_ORDER);

然后我得到了这个结果:

array(4) {
  [0]=>
  array(1) {
    [0]=>
    string(71) "SELECT SQL_CACHE
    `p`.*,
    `a`.`name`,
    (
        SELECT
            COUNT(`id`)
        FROM"
  }
  [1]=>
  array(1) {
    [0]=>
    string(6) "SELECT"
  }
  [2]=>
  array(1) {
    [0]=>
    string(61) " SQL_CACHE
    `p`.*,
    `a`.`name`,
    (
        SELECT
            COUNT(`id`)
        "
  }
  [3]=>
  array(1) {
    [0]=>
    string(4) "FROM"
  }
}

我对正则表达式没有好处。我希望获得SELECTFROM之间的所有内容。 像这样:

`p`.*,
    `a`.`name`,
    (
        SELECT
            COUNT(`id`)
        FROM
            `accounts`
        WHERE
            `id` = 1
    ) AS `counter`

COUNT(`id`)

我该怎么做?

0 个答案:

没有答案