How to split a string with a delimiter in PHP? [duplicate]

3

I have a string

"1b00bd515bf8cbc5a86f3b714361fab6"

and I want to break it down like this:

"1b00bd51-5bf8cbc5-a86f3b71-4361fab6"

HOW DO I DO IT?

    
asked by anonymous 27.04.2015 / 14:17

2 answers

5

Using the str_split() function to create an array of your string separating every 8 characters and implode() to join using a delimiter, try something like this:

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

Output:

  

1b00bd51-5bf8cbc5-a86f3b71-4361fab6

    
27.04.2015 / 14:26
0
$str = "1b00bd515bf8cbc5a86f3b714361fab6";  
print_r(preg_replace('/(\w{8})(?=\w)/', '$1-', $str));
    
27.04.2015 / 14:43