下面的文章提供了 PHP preg_split() 的概述。 preg_split() 是一個預先定義的內建函數,有助於將指定的字串轉換為陣列。這個預先定義的函數將把給定的字串分割成更小的/子字串,這些字串具有系統使用者指定的長度。此函數也很方便,例如將小字串或子字串限制為透過陣列傳回的某個限制。它類似於explode(),但唯一的區別是使用正規表示式作為分隔符,而不是explode()。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
以下是帶有參數和解釋的語法<:>
文法:
array preg_split( $pattern, $subject, $limit, $flag )
參數:
preg_split() 函式通常接受四個參數,如同上面語法中所提到的。
1. 模式:preg_split() 函數中使用的值類型是字串類型,其中模式將作為字串進行搜索,如果不是,則會分隔元素。
2。主題:$subject 參數是通常有助於儲存輸入字串的變數。
3. Limit:limit 參數將指示 preg_split() 函數的限制。如果定義了函數的限制,則僅傳回小於限制的小字串或子字串。如果限制為“0”或“-1”,則限制參數將被指定為“無限制”,然後由“$strflag”(標誌)使用。
4. Flag: flag/flags 參數有助於發出訊號,flag 的變數類型用於指示兩個狀態 TRUE 或兩個狀態 FALSE,以控制節目。
它有不同的標誌組合:
以下是範例:
這是說明 PHP 程式語言的 preg_split() 函數的範例。在下面的語法中,建立了一個變數“$inputstrVal1”並為其分配了一個字串“PavanSake”。然後透過將 preg_split() 函數使用語法中提到的基本四個參數分配給它來建立變數「$result1」。這裡「//」是模式參數,給定的輸入字串值是要拆分的主題參數,「-1」是限制值,表示沒有限制,拆分將進行到字串末尾以及通常根據我們的要求使用的普通FLAG 參數。然後“$result1”變數值將在“print_r()”函數的幫助下列印。
文法:
<?php $inputstrVal1 = 'PavanSake'; $result1 = preg_split('//', $inputstrVal1 , -1, PREG_SPLIT_NO_EMPTY); print_r($result1); ?>
輸出:
This is also one of the example of preg_split() function of PHP Programming Language. Here the phrase will be split by the number of commas. Usually the space characters are “\r”, “\t”, “\n”, “\f”. In the below example, a variable “$result11” is created by assigning the preg_split() function. It contains only 2 parameters by giving the string input “Pavan Kumar Sake” directly. Then the result of the “$result11” will be printed with the help of the “print_r()” function.
Syntax:
<?php $result11 = preg_split("/[\s,]+/", "Pavan Kumar Sake"); print_r($result11); ?>
Output:
This is the example of preg_split() function illustration of the PHP language. In the below example, at first a variable is created by assigning a string value which is actually a hyper link to provide the string input. Then again a pattern string value is given with some text and special characters. Then a result variable is created by assigning preg_split() function with 4 specific parameter variables. Then a print_r() function is used to show the array elements from the string. In the output, you will get the text in the array elements and it’s index by filtering the mentioned pattern string elements from the input string value.
Syntax:
<?php $inputstrVal1 = "http://profitloops.in/pavan/kumar/sake.php"; $patternstrVal1= "/[http:\/\/|\.]/"; $result12 = preg_split($patternstrVal1, $inputstrVal1, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE); print_r($result12 ); ?>
Output:
This is the example of showing how to split the id address using the preg_split() function. At first, echo statement is used inside the PHP tags just to print some text when the program executed. Then a variable “$ip1” is created by assigning the string value which contains the IP address. Then one more variable is created to assign the preg_split() function. This will make the string to become array elements after splitting. Then by using the print statement we are going to print the array elements using the index values.
Syntax:
<?php echo "Application :: This is the program of splitting the ip address"; $ip1 = "111.422.732.001"; $iparr1 = preg_split ("/\./", $ip1); print "$iparr1[0] <br />"; print "$iparr1[1] <br />" ; print "$iparr1[2] <br />" ; print "$iparr1[3] <br />" ; ?>
Output:
Here we saw introduction on PHP preg_split() function and it’s syntax with parameters, how the preg_split() function works in PHP along with the various examples to implement preg_split() function.
以上是PHP preg_split()的詳細內容。更多資訊請關注PHP中文網其他相關文章!