donglingyi4679
2015-04-27 12:22
浏览 42
已采纳

如何在PHP中使用分隔符拆分字符串? [关闭]

I have a string

1b00bd515bf8cbc5a86f3b714361fab6

and I want to break it down like this:

1b00bd51-5bf8cbc5-a86f3b71-4361fab6

How can I do this?

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

我有一个字符串

  1b00bd515bf8cbc5a86f3b714361fab6 
   
 
 

我希望将其分解为:

  1b00bd51-5bf8cbc5-a86f3b71-4361fab6 
  <  / pre> 
 
 

我该怎么做?

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

4条回答 默认 最新

  • dongyan2469 2015-04-27 12:27
    已采纳

    use chunk_split function split a string into smaller chunks

    below:

    $str = "1b00bd515bf8cbc5a86f3b714361fab6";
    
    $str = trim(chunk_split(str_replace('-','',$str), 8, '-'), '-');
    
    打赏 评论
  • doujia3441 2015-04-27 12:28

    You can use chunk_split () method to split a string based on fixed length. link

    The solution should look like this

    $newstr = chunk_split($string,8,"-");
    
    打赏 评论
  • dongshen2903 2015-04-27 12:33

    Try this :

    function split($str, $num, $cr)
    {
        return trim(chunk_split($str, $num, $cr), $cr);
    }
    $str = "1b00bd515bf8cbc5a86f3b714361fab6";
    echo split($str, 8, '+');
    

    Output:

    1b00bd51+5bf8cbc5+a86f3b71+4361fab6
    
    打赏 评论
  • dongweicha6077 2015-04-27 12:41

    A variation on the above but perhaps less code.

        $s='1b00bd515bf8cbc5a86f3b714361fab6';
        echo implode( '-', str_split( $s,8 ) );
    
    打赏 评论

相关推荐 更多相似问题