search
Homephp教程php手册分析PHP函数preg_match()的具体代码示例

分析PHP函数preg_match()的具体代码示例

Jun 13, 2016 am 11:07 AM
intmatchphpstrcodespecificfunctionanalyzeprototypeofExample

PHP函数preg_match()原型:int preg_match (string $pattern, string $content [, array $matches])

PHP函数preg_match()在$content字符串中搜索与$pattern给出的正则表达式相匹配的内容。如果提供了$matches,则将匹配结果放入其中。$matches[0]将包含与整个模式匹配的文本,$matches[1]将包含第一个捕获的与括号中的模式单元所匹配的内容,以此类推。该函数只作一次匹配,最终返回0或1的匹配结果数。代码6.1给出preg_match()函数的一段代码示例。
代码6.1 日期时间的匹配

<ol class="dp-xml">
<li class="alt"><span><span>//需要匹配的字符串。date函数返回当前时间  </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">content</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"Current date and time is "</font></span><span>.<br>date("Y-m-d h:i a").", </span>
</li>
<li class=""><span>we are learning PHP together.";  </span></li>
<li class="alt"><span>//使用通常的方法匹配时间  </span></li>
<li class=""><span>if (preg_match ("/d{4}-d{2}-d{2} d{2}<br>:d{2} [ap]m/", $content, $m))   </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>echo "匹配的时间是:" .$m[0]. "n";  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>//由于时间的模式明显,也可以简单的匹配  </span></li>
<li class="alt"><span>if (preg_match ("/([d-]{10}) ([d:]{5} <br>[ap]m)/", $content, $m))   </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>echo "当前日期是:" .$m[1]. "n";  </span></li>
<li class=""><span>echo "当前时间是:" .$m[2]. "n";  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span><?php <br />
</li>
</ol>

这是一个简单动态文本串匹配实例。假设当前系统时间是“2006年8月17日13点25分”,将输出如下的内容。
匹配的时间是:2006-08-17 01:25 pm
当前日期是:2006-08-17
当前时间是:01:25 pm
以上就是PHP函数preg_match()的具体功能示例,希望大家能够掌握。


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version