下面的文章提供了 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中文网其他相关文章!