PHP preg_match()函数

PHP preg_match()函数

int preg_match (string pattern, string string [, array pattern_array], [, int flags [, intoffset]]]);

定义和用法

preg_match()函数搜索字符串中的模式,如果存在则返回true,否则返回false。

如果提供了可选的输入参数pattern_array,则pattern_array将包含搜索模式中所包含子模式的各个部分。

如果传递了PREG_OFFSET_CAPTURE标志,则每次匹配时也会返回相应字符串的偏移量。

通常,搜索从主字符串的开头开始。可选的offset参数可用于指定开始搜索的替代位置。

返回值

  • 如果模式存在,则返回true,否则返回false。

示例

以下是一段代码,将此代码复制粘贴到一个文件中并验证结果。

<?php
   line = "Vi is the greatest word processor ever created!";
   // perform a case-Insensitive search for the word "Vi"

   if (preg_match("/\bVi\b/i",line, $match)) :
      print "Match found!";
      endif;
?>

这将产生以下结果−

Match found!

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程