How to split a string with delimiter in PHP?

Asked

Viewed 635 times

3

have a string

"1b00bd515bf8cbc5a86f3b714361fab6"

and I want to share it that way:

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

HOW DO I DO?

  • Other options such as adding a character in a specific range http://answall.com/q/44639/91

2 answers

5


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

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

Exit:

1b00bd51-5bf8cbc5-a86f3b71-4361fab6

0

$str = "1b00bd515bf8cbc5a86f3b714361fab6";  
print_r(preg_replace('/(\w{8})(?=\w)/', '$1-', $str));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.