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

preg_split

通過一個正則表達式分隔字符串
名稱:preg_split
分類:正則處理PCRE
所屬語言:php
一句話介紹:使用正則表達式的匹配項作為分隔符將字符串拆分為數組。

定義和用法

preg_split()函數使用正則表達式的匹配項作為分隔符將字符串分割成數組。

實例

例子1

使用preg_split() 將日期拆分為其組成部分:

 <?php
$date = "1970-01-01 00:00:00" ;
$pattern = "/[-\s:]/" ;
$components = preg_split ( $pattern , $date ) ;
print_r ( $components ) ;
?>

親自試一試

例子2

使用PREG_SPLIT_DELIM_CAPTURE 標誌:

 <?php
$date = "1970-01-01 00:00:00" ;
$pattern = "/([-\s:])/" ;
$components = preg_split ( $pattern , $date , - 1 ,
PREG_SPLIT_DELIM_CAPTURE ) ;
print_r ( $components ) ;
?>

親自試一試

例子3

使用PREG_SPLIT_OFFSET_CAPTURE 標誌:

 <?php
$date = "1970-01-01" ;
$pattern = "/-/" ;
$components = preg_split ( $pattern , $date , - 1 ,
PREG_SPLIT_OFFSET_CAPTURE ) ;
print_r ( $components ) ;
?>

親自試一試

同類函數
  • 返回匹配模式的數組條目 preg_grep

    preg_grep

    返回匹配模式的數組條目
  • 執行正則表達式匹配 preg_match

    preg_match

    執行正則表達式匹配
  • 通過一個正則表達式分隔字符串 preg_split

    preg_split

    通過一個正則表達式分隔字符串
  • 返回最後一個PCRE正則執行產生的錯誤代碼 preg_last_error

    preg_last_error

    返回最後一個PCRE正則執行產生的錯誤代
  • 執行正則表達式搜索和替換 preg_filter

    preg_filter

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

    preg_last_error_msg

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

    preg_match_all

    執行全局正則表達式匹配
  • 執行正則表達式搜索並使用回調替換 preg_replace_callback_array

    preg_replace_callback_array

    執行正則表達式搜索並使用回調替換
熱門文章