【问题标题】:How to limit the elements created by explode()如何限制explode()创建的元素
【发布时间】:2011-12-10 13:33:18
【问题描述】:

如果我有一个类似的字符串

“1234 额外名称”

我愿意

list($postcode, $city) = explode(" ", $string);

原来 $postcode 是 "1234" 而 $city 是 "Name"。我希望 $city 成为“Name Extra”

我该怎么做?

【问题讨论】:

标签: php limit explode


【解决方案1】:

使用explode的第三个参数($limit)。结果数组将包含 $limit 个元素。

list($postcode, $city) = explode(" ", $string, 2);

【讨论】:

    【解决方案2】:
    list($postcode, $city) = explode(" ", $string, 2);
    

    【讨论】:

    • 请始终在您的答案中发布一些类似的解释。纯代码答案对教育未来的读者几乎没有作用。
    【解决方案3】:

    如果您查看PHP docs,您会看到explode() 有一个可选的第三个参数,它指定您想要爆炸的次数。

    【讨论】:

      【解决方案4】:

      来自手册:

      array explode ( string $delimiter , string $string [, int $limit ] )
      

      所以你使用可选的限制参数,在你的情况 2 中,我相信

      【讨论】:

        猜你喜欢
        • 2019-01-01
        • 1970-01-01
        • 2018-05-21
        • 1970-01-01
        • 2016-03-15
        • 1970-01-01
        • 2011-03-29
        • 1970-01-01
        • 2010-11-29
        相关资源
        最近更新 更多