【问题标题】:Explode PHP Function [closed]展开 PHP 函数 [关闭]
【发布时间】:2012-10-05 04:28:18
【问题描述】:

有什么办法可以炸掉这个字符串?

$img_name = "123_black_bird_aaaa";
explode("_", $img_name);

现在这样的图像名称包含多个下划线。我怎样才能在第一个下划线时爆炸它而不关心剩余的字符串有多少下划线?

$img_name = "123_black_bird_aaaa";
$array = explode("_", $img_name);       
$first_underscore_part = $array[0];
$remaining_string      = $array[1];

例如姓名"123_black_bird_aaaa"

现在我希望 "123_" 在数组的索引 0"black_bird_aaaa" 在数组的索引 1

【问题讨论】:

标签: php arrays string


【解决方案1】:

这只是一个注释,参数列表来自:http://php.net/explode

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

不要赞成这个答案,而是反对这个问题和/或投票关闭并删除它。谢谢!

【讨论】:

  • 我喜欢你说不要赞成你的答案,但人们仍然在这样做。
  • 那是因为,正如在这个网站上多次证明的那样,人们无法阅读......虽然我意识到爆炸不会起作用,因为它省略了第一个 _ OP 实际上是从技术上看保持explode 是错误的答案。
  • @Sammaye:对不起:list($first, $last) = explode('_', $string, 2) + array(NULL, NULL) and $last !== NULL and $first .= '_';
【解决方案2】:
<?php
$img_name = explode("_", $img_name,2);
print_r($img_name);
?>

【讨论】:

    【解决方案3】:

    不用explode也可以。

    $pos = strpos($img_name, "_"); //finds the first underscore position
    if ($pos === false)
    { //we have an underscore
     $firstPart = substr(0 , $pos , $img_name);
     //Get the characters before the position of the first underscode
    }
    

    如果你仍然想要explode,@hakre 的答案是 AWSOME。

    【讨论】:

      【解决方案4】:

      您可以通过使用explode函数将limit参数的值作为参数传递给第一个分隔符来拆分字符串。

      下面是使用explode函数在第一个分隔符处拆分字符串的代码sn-p

          $delimiter='_';
          $img_name = "123_black_bird_aaaa";
          $result=explode($delimiter,$img_name,2);
      

      【讨论】:

        猜你喜欢
        • 2010-11-21
        • 2013-04-26
        • 2020-09-08
        • 2022-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多