首页  >  文章  >  后端开发  >  PHP preg_split()

PHP preg_split()

WBOY
WBOY原创
2024-08-29 12:50:41827浏览

下面的文章提供了 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,以控制节目。

它有不同的标志组合:

  • PREG_SPLIT_NO_EMPTY:如果标志参数/变量设置为“PREG_SPLIT_NO_EMPTY”,则 preg_split() 函数将仅返回非空片段。
  • PREG_SPLIT_DELIM_CAPTURE:如果标志参数设置为“PREG_SPLIT_DELIM_CAPTURE”,则将返回并捕获括号表达式的分隔符模式。
  • PREG_SPLIT_OFFSET_CAPTURE: 如果标志参数设置为“PREG_SPLIT_OFFSET_CAPTURE”,则对于每个出现的匹配字符串偏移量将被返回,并且还有助于更改数组的返回值,其中匹配的字符串偏移量将设置为“0”值,输入字符串偏移值将设置为值“1”。

preg_split() 函数在 PHP 中如何工作?

  • preg_split() 函数将有助于在分割边界匹配时返回数组。
  • 只有当原始数组或字符串超出限制时,preg_split() 函数才会返回数组元素,因为条件为 TRUE,否则为 FALSE。
  • Preg_split() 函数的工作原理是从提供的原始字符串文本中分割一些术语/文本/特殊字符,然后将其存储在特定提到的变量名称的索引值中,该索引值只不过是在一个数组中。
  • 可以调用使用数组索引值(如 array[0] 等)分割的所有值。

PHP preg_split() 示例

以下是示例:

示例#1

这是说明 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);
?>

输出:

PHP preg_split()

Example #2

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:

PHP preg_split()

Example #3

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:

PHP preg_split()

Example #4

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:

PHP preg_split()

Conclusion

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP strtoupper()下一篇:PHP substr_replace()