30 September 2023

Split string with characters in PHP. You can achieve this by taking a string $str, split it into chunks of 2 characters each with colons in between, and then remove any trailing colon from the resulting string. The final output will be a string with pairs of characters separated by colons.

Source code viewer
  1. $str = '12345';
  2. $str = rtrim(chunk_split($str, 2, ':'), ':');
  3. // Output: 12:34:5
Programming Language: PHP