一種逆天的PHP字串構造方法.

來源:互聯網
上載者:User
雖然PHP有<<<這種多行字串的用法,但用著不爽啊,因為最後的結束行不能有空格。看看我寫的這種字串構造方法吧,代碼如下:

/**把一個匿名函數的注釋轉換為字串     * @param function $closure 匿名函數     * @param array $replace 替換佔位字元的內容,佔位字元的格式正則表現為{[0-9]+},比如{0}、{1} 其中數值對應$replace的索引     * @return string 返回匿名函數內的注釋內容     */    function string($closure,$replace=NULL){        $func = new ReflectionFunction($closure);        $start = $func->getStartLine() - 1;$end =  $func->getEndLine() - 1;$filename = $func->getFileName();$code=implode("", array_slice(file($filename),$start, $end - $start + 1));        $reg="/\\/\\*([^\\0]+)\\*\\//";        $match=array();        preg_match($reg,$code,$match);        if(count($match)>0){            if(!$replace){                return preg_replace("/\\\\\\\/","\\",preg_replace("/\*(\\\\\/)/", "*/", trim($match[1])));            }            if(count($replace)<=0){                return preg_replace("/\\\\\\\/","\\",preg_replace("/\*(\\\\\/)/", "*/", trim($match[1])));            }            $tmp1=$match[1];            for($i=0;$i  


用法:

$sql=string(function(){/*            select * from table1 T1            left join table2 T2 on T2.id=T1.id            where T1.id={0} && T2.name='{1}'        */},array(101,"ioriliao"));

輸出結果:

select * from table1 T1left join table2 T2 on T2.id=T1.idwhere T1.id=101 && T2.name='ioriliao'



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.