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

PHP 爆炸()

WBOY
WBOY原创
2024-08-29 12:55:291002浏览

PHP编程语言的explode()函数是一个内置函数,它有助于将一个字符串分割成许多不同的字符串。 explode() 函数有助于根据字符串的分隔符分割字符串,这意味着它将在分隔符字符出现/出现的任何地方分割字符串。这个explode()函数将返回一个数组,其中包含分割原始字符串后形成的字符串/字符串。 PHP的explode()函数通常只接受三个参数,将一个字符串分割成许多字符串元素并存储到一个数组中。就像把大绳子切成许多小绳子等等。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

explode(separator1, OriginalString1, No.ofElements1)

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

PHP编程语言的explode()函数基于上述三个参数工作。它们是 Separator1、OriginalString1 和 No.ofElements1 参数。它的工作原理是借助explode()函数内的参数将字符串分割成更小的字符串。所有这些较小的字符串元素都存储在带有索引值的数组中,并且还基于explode()函数的参数。

参数说明:

explode() 函数的参数实际上接受三个参数,但其中只有两个参数是必需的,一个参数是可选参数。

1。 Separator1/Delimeter:PHP编程语言的Separator1参数实际上指定了字符串必须分割的一些临界点,这意味着每当在字符串元素中找到该字符串字符时,它就象征着结束一个数组元素的开头和另一个数组元素的开头。此 delimeter/Separator1 参数为必填参数。

2。 OriginalString1: PHP 编程语言的 OriginalString1 参数实际上是一个原始字符串,用于将一个字符串拆分为多个字符串,并且仅当字符串内部存在可用字符时才将其存储在数组中。这个OriginalString1参数也是必选参数。

3。 No.ofElements1/Limit: 该参数为可选参数,非强制。此参数有助于指定数组元素的数量。该参数可以是任意类型的整数。

可以是正整数、负整数或零整数。

  • 正整数 (N):如果 No.ofElements1 参数以正整数值传递,则意味着数组将包含此数量的字符串元素。如果将元素与分隔符分隔后的元素数量将出现大于 N-1 个元素的值,但保持不变,最后一个字符串元素将是整个剩余的字符串。
  • 负整数 (N):如果将负整数传递给 No.ofElements1 参数,则最后一个字符串元素 N 将被修剪,剩余的数组元素将仅作为一个字符串返回。
  • 零整数:如果 No.ofElements1 参数以“零”值传递,则数组将仅返回一个字符串元素,即完整字符串(整个字符串)。当没有值传递给此 No.ofElements1 参数时,返回的数组将具有使用分隔符分隔字符串后形成的元素总数。

返回值类型:

explode() 函数的返回值类型是带有字符串列表的单个数组。

PHPexplode() 示例

下面给出的是提到的示例:

示例#1

这是考虑字符串之间的空格将单个字符串拆分为许多小字符串的示例。在下面的 PHP 标签示例中,创建了一个变量“$s1”,并将一个字符串句子分配给 $s1 变量。然后print_r()函数与其中的explode()函数一起使用。这里separator1/delimeter参数是空格“ ”,$s1是输入参数/原始字符串,然后这里没有提到任何参数。这里没有 limit/No.ofElements1 参数,因此字符串的分割没有限制。分割后,数组索引值内部将存储小字符串,并在 print_r() 函数的帮助下打印。

语法:

<?php
$s1 = "My Name is Pavan Kumar Sake";
print_r (explode(" ",$s1));
?>

输出:

PHP 爆炸()

Example #2

This is the example of implementing with limit value “0”. At first, a variable “$stra” is created and this variable is also assigned with a string value ‘car1, bus1, motorbike1, cycle1’. Then print_r() function is made along with the explode() function in it. In the explode() function, “,” is used as separator1 parameter, $stra variable value is used as OriginalString1 Parameter and value “0” is used as No.ofElements1 Parameter. It is mentioned in the parameters description that if the No.ofElements1/Limit value is mentioned as 0 then the whole original string is considered as one single string array element. This will be printed as shown in the output. Then print function is used to print line break.

Syntax:

<?php
$stra = 'car1, bus1, motorbike1, cycle1';
print_r(explode(',',$stra,0));
?>

Output:

PHP 爆炸()

Example #3

This is the example of implementing the string splitting with the help of the positive integer as No.ofElements1/limit Parameter. Here at first, a string variable called “$strab” is created with the string value ‘car1, bus1, motorbike1, cycle1’. Then print_r() function is used along with the explode() function along with the three parameters. Here “,” is the Separator1 parameter, “$strab” is the original string element which is nothing but OriginalString1 parameter, “2” is the No.ofElements1/limit Parameter. According to the parameters description, if the positive integer value is passed then n-1 array indexes values will splitted and stored and for N-1 index value, remaining whole string will be printed.

Syntax:

<?php
$strab = 'car1, bus1, motorbike1, cycle1';
print_r(explode(',',$strab,2));
?>

Output:

PHP 爆炸()

Example #4

This is the program of implementing the string splitting function by using different type of integer values for No.ofElements1/limit parameter. So that one can know what actually happens for different parameters which are acting inside of the explode() function. For the first explode() function, the whole original string is considered as only one array element. Then value “4” is used for the second explode() function. For this, n-1=3 array indexes string values will be printed but for n-1 array index the whole remaining string will be printed. Then for the third explode() function, negative integer value(-N) is used. So at N array index values the string will be trimmed and will be printed starting from the 0 index value.

Syntax:

<?php
$Original_str1 = "Hello, IamPavan Kumar Sake-Write, BusinessMan, PhysicsNerd.";
print_r (explode (" ",$Original_str1, 0));
print "\n";
print_r (explode (" ",$Original_str1, 4));
print "\n";
print_r (explode (" ",$Original_str1, -3));
print "\n";
?>

Output:

PHP 爆炸()

以上是PHP 爆炸()的详细内容。更多信息请关注PHP中文网其他相关文章!

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