Code collection--php--Loading SQL files and parsing an array

Source: Internet
Author: User

PHP loads the SQL file and parses it into a semicolon-delimited array. (Supports stored procedures and function extraction, automatic filtering of annotations)  

/** * Load SQL file as semicolon-delimited array * <br/> supports stored procedure and function extraction, automatically filters comments * <br/> For example: Var_export (Load_sql_file (' Mysql_routing_ Example/fn_cdr_parse_accountcode.sql ')); * @param string $path file path * @return Boolean|array * @since 1.0 <2015-5-27> Sochishun Added. */functionLoad_sql_file ($path,$FN _splitor= ';; ') {    if(!file_exists($path)) {        return false; }    $lines=file($path, File_ignore_new_lines |file_skip_empty_lines); $aout=false; $str= ' '; $skip=false; $FN=false; foreach($lines  as $line) {        $line=Trim($line); //Filter Comments        if(!$line|| 0 = = =Strpos($line, '---') | | 0 = = =Strpos($line, ' * ') | | 0 = = =Strpos($line, '/* ') | | (false!==Strpos($line, ' * * ') &&strlen($line) == (Strpos($line, ' * * ') + 2))) {            if(!$skip&& 0 = = =Strpos($line, ‘/*‘)) {                $skip=true; }            if($skip&&false!==Strpos($line, ' * * ') &&strlen($line) == (Strpos($line, ' * * ') + 2)) {                $skip=false; }            Continue; }        if($skip) {            Continue; }        //extracting stored procedures and functions        if(0 = = =Strpos($line, ' DELIMITER '.$FN _splitor)) {            $FN=true; Continue; }        if(0 = = =Strpos($line, ' DELIMITER; ')) {            $FN=false; $aout[] =$str; $str= ' '; Continue; }        if($FN) {            $str.=$line. ‘ ‘; Continue; }        //extracting normal statements        $str.=$line; if(false!==Strpos($line, ‘;‘) &&strlen($line) == (Strpos($line, ‘;‘) + 1)) {            $aout[] =$str; $str= ' '; }    }    return $aout;}

Copyright NOTICE: This document is licensed under the attribution-Non-commercial use-sharing (CC BY-NC-SA 3.0 CN) International License Agreement, please specify the author and source.
Title: Code Collection series--php--loading SQL files and parsing an array
This article link: http://www.cnblogs.com/sochishun/p/7061588.html
This article Sochishun (e-mail: 14507247#qq.com | blog: http://www.cnblogs.com/sochishun/)
Published: June 23, 2017

Code collection--php--Loading SQL files and parsing an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.