How can I stop explode function after certain index.
For example
<?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);
?>
What if want to use only first 10 elements ($result[10])
How can I stop explode function once 10 elements are filled.
One way is to first trim the string upto first 10 spaces (” “)
Is there any other way, I don’t want to store the remaining elements after limit anywhere (as done using positive limit parameter)?
What’s about that third parameter of the function?
check out the
$limitparameter.Manual: http://php.net/manual/en/function.explode.php
An example from the manual:
The above example will output:
In your case:
According to the php manual , when you’re using the
limitparameter: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).