Regular expression to remove comments from SQL statement

后端 未结 8 1785
时光说笑
时光说笑 2020-12-15 12:51

I\'m trying to come up with a regular expression to remove comments from an SQL statement.

This regex almost works:

(/\\*([^*]|[\\r\\n]|(\\*+([^*/]|         


        
8条回答
  •  半阙折子戏
    2020-12-15 12:58

    This code works for me:

    function strip_sqlcomment ($string = '') {
        $RXSQLComments = '@(--[^\r\n]*)|(\#[^\r\n]*)|(/\*[\w\W]*?(?=\*/)\*/)@ms';
        return (($string == '') ?  '' : preg_replace( $RXSQLComments, '', $string ));
    }
    

    with a little regex tweak it could be used to strip comments in any language

提交回复
热议问题