Home  >  Article  >  Backend Development  >  Interpretation of relevant code examples for PHP regular expression multi-line matching_PHP tutorial

Interpretation of relevant code examples for PHP regular expression multi-line matching_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:31:01937browse

In our study, many problems need to be solved continuously in practice. When we match PHP regular expressions, it is difficult to perform complex matching operations only by using regular table functions under POSIX. So how to implement multi-line matching of PHP regular expressions.

For example, perform a match search on the entire file (especially multi-line text). One way to do this using ereg() is to do it in separate lines. The example in Listing 6.5 demonstrates how ereg() assigns the parameters of the INI file to an array.

Code 6.5 PHP regular expression multi-line matching of file content

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>$</span><span class="attribute">rows</span><span> = </span><span class="attribute-value">file</span><span>('php.ini'); //将php.ini文件读到数组中  </span></li><li class="alt"><span>//循环遍历  </span></li><li><span>foreach($rows as $line)  </span></li><li class="alt"><span>{  </span></li><li><span>If(trim($line))  </span></li><li class="alt"><span>{  </span></li><li><span>//将匹配成功的参数写入数组中  </span></li><li class="alt"><span>if(eregi("^([a-z0-9_.]*) *=(.*)", $line, $matches))  </span></li><li><span>{  </span></li><li class="alt"><span>$options[$matches[1]] = trim($matches[2]);  </span></li><li><span>}  </span></li><li class="alt"><span>unset($matches);  </span></li><li><span>}  </span></li><li class="alt"><span>}  </span></li><li><span>//输出参数结果  </span></li><li class="alt"><span>print_r($options);  </span></li><li><span class="tag">?></span><span> </span></span></li></ol>

The above code example is the specific implementation method of PHP regular expression multi-line matching. I hope you can learn something after reading it. experience.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446248.htmlTechArticleIn our study, many problems need to be solved continuously in practice. When we match PHP regular expressions, it is difficult to use only the regular expression functions under POSIX...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn