浅析PHP正则表达式的应用实例
PHP正则表达式的应用有很多,那么我们在实际开发应用中会遇到提取字符串、高级的代换的功能,那么如何实现呢?我们来看看具体的过程。
PHP正则表达式的应用之提取字符串介绍:
ereg() and eregi()
PHP正则表达式的应用之提取字符串实施:
有一个特性是允许用户通过正则表达式去提取字符串的一部分(具体用法你可以阅读手册).
比如说,我们想从 path/URL 提取文件名 – 下面的代码就是你需要:
<ol class="dp-c"> <li class="alt"><span><span class="func">ereg</span><span>(</span><span class="string">"([^\/]*)$"</span><span>, </span><span class="vars">$pathOrUrl</span><span>, </span><span class="vars">$regs</span><span>); </span></span></li> <li> <span class="func">echo</span><span> </span><span class="vars">$regs</span><span>[1]; </span> </li> </ol>
PHP正则表达式的应用之高级的代换介绍:
ereg_replace() 和 eregi_replace()也是非常有用的
PHP正则表达式的应用之高级的代换实施:
假如我们想把所有的间隔负号都替换成逗号:
<ol class="dp-c"><li class="alt"><span><span class="func">ereg_replace</span><span>(</span><span class="string">"[ nrt]+"</span><span>, </span><span class="string">","</span><span>, trim(</span><span class="vars">$str</span><span>)); </span></span></li></ol>
PHP正则表达式的应用的相关介绍就向你讲解到这里,希望对你了解和掌握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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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.