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

I have a string

1b00bd515bf8cbc5a86f3b714361fab6

and I want to break it down like this:

1b00bd51-5bf8cbc5-a86f3b71-4361fab6

How can I do this?

use chunk_split function split a string into smaller chunks

below:

$str = "1b00bd515bf8cbc5a86f3b714361fab6";

$str = trim(chunk_split(str_replace('-','',$str), 8, '-'), '-');

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

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

A variation on the above but perhaps less code.

    $s='1b00bd515bf8cbc5a86f3b714361fab6';
    echo implode( '-', str_split( $s,8 ) );