當前位置: 首頁> 函數類別大全> preg_match

preg_match

執行正則表達式匹配
名稱:preg_match
分類:正則處理PCRE
所屬語言:php
一句話介紹:在字符串中查找模式的第一個匹配項。

定義和用法

preg_match()函數返回是否在字符串中找到匹配項。

實例

例子1

使用正則表達式對字符串中的"w3school" 進行不區分大小寫的搜索:

 <?php
$str = "Visit W3School" ;
$pattern = "/w3school/i" ;
echo preg_match ( $pattern , $str ) ;
?>

親自試一試

例子2

使用PREG_OFFSET_CAPTURE 找到輸入字符串中匹配項的位置:

 <?php
$str = "Welcome to W3School" ;
$pattern = "/w3school/i" ;
preg_match ( $pattern , $str , $matches , PREG_OFFSET_CAPTURE ) ;
print_r ( $matches ) ;
?>

親自試一試

同類函數
  • 執行全局正則表達式匹配 preg_match_all

    preg_match_all

    執行全局正則表達式匹配
  • 返回上一次PCRE正則表達式執行的錯誤消息 preg_last_error_msg

    preg_last_error_msg

    返回上一次PCRE正則表達式執行的錯誤消
  • 執行正則表達式搜索和替換 preg_filter

    preg_filter

    執行正則表達式搜索和替換
  • 執行一個正則表達式搜索並且使用一個回調進行替換 preg_replace_callback

    preg_replace_callback

    執行一個正則表達式搜索並且使用一個回調進
  • 通過一個正則表達式分隔字符串 preg_split

    preg_split

    通過一個正則表達式分隔字符串
  • 執行一個正則表達式的搜索和替換 preg_replace

    preg_replace

    執行一個正則表達式的搜索和替換
  • 轉義正則表達式字符 preg_quote

    preg_quote

    轉義正則表達式字符
  • 返回最後一個PCRE正則執行產生的錯誤代碼 preg_last_error

    preg_last_error

    返回最後一個PCRE正則執行產生的錯誤代
熱門文章