解读PHP正则表达式多行匹配的相关代码示例
在的学习中,很多问题都需要我们在实践中不断的解决。我们在进行PHP正则表达式的匹配的时候,仅仅使用POSIX下的正则表式函数,很难进行复杂的匹配操作,那么该如何实现PHP正则表达式多行匹配呢。
例如,对整个文件(尤其是多行文本)进行匹配查找。使用ereg()对此进行操作的一个方法是分行处理。代码6.5的示例演示了ereg()如何将INI文件的参数赋值到数组之中。
代码6.5 文件内容的PHP正则表达式多行匹配
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></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> </li> </ol>
以上代码示例就是PHP正则表达式多行匹配的具体实现方法,希望大家看后能有所体会。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools