Dea*_*ock 3 php arrays explode
如何在某个索引后停止爆炸功能.例如
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
如果只想使用前10个元素怎么办($ result [10])如果填充了10个元素,怎么能停止爆炸功能呢?
一种方法是先将字符串修剪到前10个空格("")
有没有其他方法,我不想在限制后的任何地方存储剩余的元素(使用正限制参数完成)?
Ofi*_*uch 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('|', $str, -1));
?>
Run Code Online (Sandbox Code Playgroud)
The above example will output:
Array ( [0] => one [1] => two|three|four ) Array ( [0] => one [1] => two [2] => three )
In your case:
print_r(explode(" " , $test , 10));
Run Code Online (Sandbox Code Playgroud)
According to the php manual , when you're using the limit parameter:
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.
Therefore , you need to get rid of the last element in the array.
You can do it easily with array_pop (http://php.net/manual/en/function.array-pop.php).
$result = explode(" " , $test , 10);
array_pop($result);
Run Code Online (Sandbox Code Playgroud)