停止某些指标后爆炸 [英] Stop explode after certain index

查看:174
本文介绍了停止某些指标后爆炸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能阻止某些指标后爆炸功能。
例如:

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);
?>

如果希望只使用前10个元素是什么($结果[10])
我怎么能阻止爆炸函数一次10个元素填充。

What if want to use only first 10 elements ($result[10]) How can I stop explode function once 10 elements are filled.

的一种方法是首先修剪串高达第一10个空格()

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?

阵爆炸(字符串分隔符$,$弦弦[摘要$上限])

array explode ( string $delimiter , string $string [, int $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));
?>

在上面的例子将输出:

The above example will output:

阵列(
      [0] =>单
      [1] =>二|三|四)阵列(
      [0] =>单
      [1] =>双
      [2] =>三级)

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

在您的情况:

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

据PHP手册中,当你使用限制参数:

如果限制设置和积极的,返回的数组将包含
  最大限制元件与含的其余部分的最后一个元素
  字符串。

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)。

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);

这篇关于停止某些指标后爆炸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆