Regular expression to split SQL statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marian Heddesheimer

    #1

    Regular expression to split SQL statement

    Hi,

    does anybody know a good pattern for PHP regular expression (like
    preg_match) to split an existing SQL statement into their parts?

    For example, if I have this:

    $sql = "select field1, field2 from tab1, tab2 where tab1.id = tab2.id
    order by tab2.title, tab1.id";

    I can use this to split the statement:

    $pattern="/(select)(.*)(fr om)(.*)(where)( .*)(order by)(.*)/";
    preg_match($pat tern, $sql, $ary);

    So I will get my field list, table list, where statement and order
    statement into separate array elements.

    What I search is a pattern that will handle optional statements as
    well. For example if I leave out the "order by", my pattern won't
    work. I also would like to use the same pattern on "left join" and
    "having" within the sql statement.

    So maybe, somebody out there has already developed such an expression
    and can help me out here.

    Thanks in advance

    Marian

    --
    Internet-Dienstleistunge n - von der Webseite bis zum Online-Shop
    http://www.heddesheimer.de mailto:marian@h eddesheimer.de
Working...