特定索引后停止爆炸
问题描述:
如何在特定索引后停止爆炸功能。 例如特定索引后停止爆炸
<?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个元素($结果[10]) 我怎么能阻止爆炸函数一次10件充满。
一种方法是先修剪串高达前10位(”“)
是否有任何其他的方式,我不想存储限制的任何地方后剩余的元素做(使用正极限参数的做法)?
答
这个函数的第三个参数是什么?
阵列爆炸(字符串$分界,字符串$串[摘要$极限])
检查出$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('|', $str, -1));
?>
上例将输出:
阵列( [0] =>一个 [1] = >两个|三个|四个)阵列( [0] =>一个 [1] =>两个 [2] =>三个)
你的情况:
print_r(explode(" " , $test , 10));
根据PHP手册中,当你使用limit
参数:
如果限制设置和积极的,返回的数组将包含一个 限制元素的最大值,最后一个元素包含 字符串的其余部分。
因此,您需要摆脱数组中的最后一个元素。 您可以使用array_pop
(http://php.net/manual/en/function.array-pop.php)轻松完成。
$result = explode(" " , $test , 10);
array_pop($result);
谢谢...有没有办法丢弃在正极限下一个元素(这来限制之后)? – Deadlock
没有得到你,你能举个例子吗? –
像你在你的例子中的正极限 [1] => two | three | 4 我不想将这个元素(Array [1])存储在任何地方,这应该被丢弃 我只想要 Array([0 ] =>一个) 而不是 Array([0] => one [1] => two | three | four) – Deadlock