php – 在某些索引后停止爆炸

前端之家收集整理的这篇文章主要介绍了php – 在某些索引后停止爆炸前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在某个索引后停止爆炸功能.
例如
<?PHP
        $test="The novel Prognosis Negative by Art Vandelay expresses protest against many different things. The story covers a great deal of time and takes the reader through many different places and events,as the author uses several different techniques to really make the reader think. By using a certain type of narrative structure,Vandelay is able to grab the reader’s attention and make the piece much more effective and meaningful,showing how everything happened";

    $result=explode(" ",$test);
    print_r($result);
?>

如果只想使用前10个元素怎么办($result [10])
一旦填充了10个元素,如何停止爆炸功能.

一种方法是先将字符串修剪到前10个空格(“”)

有没有其他方法,我不想将限制后的剩余元素存储在任何地方(使用正限制参数完成)?

那个函数的第三个参数是什么?

array explode ( string $delimiter,string $string [,int $limit ] )

@H_502_18@

看看$limit参数.

手册:http://php.net/manual/en/function.explode.php

手册中的一个例子:

<?PHP
$str = 'one|two|three|four';

// positive limit
print_r(explode('|',$str,2));

// negative limit (since PHP 5.1)
print_r(explode('|',-1));
?>

以上示例将输出

Array (
[0] => one
[1] => two|three|four ) Array (
[0] => one
[1] => two
[2] => three )

@H_502_18@

在你的情况下:

print_r(explode(" ",$test,10));

根据PHP手册,当你使用limit参数时:

If limit is set and positive,the returned array will contain a
maximum of limit elements with the last element containing the rest of
string.

@H_502_18@

因此,您需要摆脱数组中的最后一个元素.
你可以使用array_pop(http://php.net/manual/en/function.array-pop.php)轻松完成.

$result = explode(" ",10);
array_pop($result);
原文链接:https://www.f2er.com/php/139015.html

猜你在找的PHP相关文章