A php string construction method against the day

Source: Internet
Author: User
A php string construction method against the day. although PHP has the usage of multi-line strings such as & lt;, it is uncomfortable to use because the last end line cannot be empty & #26684 ;. Let's take a look at the string construction method I wrote. the code is as follows: ** convert an anonymous function annotation to a string * @ paramfunction $ closure anonymous function * @ paramarray $ replace a PHP string construction method against the day.

Although PHP has < <这种多行字符串的用法,但用着不爽啊,因为最后的结束行不能有空格。看看我写的这种字符串构造方法吧,代码如下:< p>

/** Convert the annotation of an anonymous function to the string * @ param function $ closure anonymous function * @ param array $ replace to replace the placeholder character content, the regular expression of placeholder characters is {[0-9] + }, for example, in {0} and {1}, the value corresponds to the $ replace Index * @ return string and returns the comments in the anonymous function */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), $ star T, $ 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
 
  


Usage:

$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"));

Output result:

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



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.