加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:52:22 所属栏目: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 event
如何在某个索引后停止爆炸功能.
例如
<?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 ] )

看看$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 )

在你的情况下:

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.

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

$result = explode(" ",10);
array_pop($result);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读