douyin8809
2015-02-05 15:27
浏览 35
已采纳

如何将2个字符串与分隔符连接在一起? [关闭]

I have a string like this $value[0] = "8000297c - 1360598144 "

I explode them into 2 pieces by doing this : $elements = explode('-', $value[0]);

Then I created 2 variables to store them :

$first = strtolower($elements[0]); // 8000297c 
$last  = strtoupper($elements[1]); // 1360598144

Now- after formatting them, I want to join them back to the original format like this

  • $first."-".$last

I was thinking to use implode() function, and I tried

  • $polish_sku = implode("-", $first, $last); - then, I got this :

enter image description here

I hope someone don't mind and point out what I did wrong. :D

图片转代码服务由CSDN问答提供 功能建议

我有一个像这样的字符串 $ value [0] =“8000297c - 1360598144”

我将它们分成两部分: $ elements = explode(' - ',$ value [0]);

然后我创建了2个变量来存储它们:

  $ first = strtolower($ elements [0]);  // 8000297c 
 $ last = strtoupper($ elements [1]);  // 1360598144 
   
 
 

现在 - 在格式化之后,我想将它们连接回原始格式,如下所示 \ n

  • $ first。“ - ”。$ last

    我正在考虑使用< code> implode()函数,我试过

    • $ polish_sku = implode(“ - ”,$ first,$ last); < / code> - 然后,我得到了这个:

      我希望有人不介意并指出我做错了什么。 :D

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duanliaouu965826 2015-02-05 15:29
    已采纳

    if you want to use implode

    implode("-", array($first, $last));
    

    or just concat them ?

    $first . "-" . $last
    

    or use printf/sprintf ?

    printf("%s-%s", $first, $last)
    
    打赏 评论
  • dongxinyue2817 2015-02-05 15:28
    $polish_sku = implode("-", array($first, $last)); 
    

    would work if you want to use implode

    打赏 评论
  • douan3019 2015-02-05 15:29
    $polish_sku = implode("-", $first, $last); 
    

    should be

    $polish_sku = implode("-", array($first, $last));
    

    Moreover you can simply take advantage of string concatenation like

    $polish_sku = $first.'-'.$last
    

    or

    $polish_sku = sprintf("%s-%s", $first, $last);
    
    打赏 评论
  • dongzhouzhang8696 2015-02-05 15:30
    $polish_sku = implode("-", $elements);
    

    if $elements have more than two elements:

    $polish_sku = implode("-", array($elements[0],$elements[1]));
    
    打赏 评论
  • dongzhuo1930 2015-02-05 15:35

    Try this:

    $polish_sku = implode("-", array($first, $last));
    

    implode takes an array as the second parameter.

    打赏 评论

相关推荐 更多相似问题