Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmu83/417c3874e797ba35a2b23434885f66af to your computer and use it in GitHub Desktop.
Save ahmu83/417c3874e797ba35a2b23434885f66af to your computer and use it in GitHub Desktop.
<?php
/**
* Break a string into
* n number of equal
* parts to an array
*/
$string = 'This-is-a-long-string-that-has-some-random-text-with-hyphens!';
$string_length = strlen($string);
switch ($string_length) {
case ($string_length > 0 && $string_length < 21):
$parts = ceil($string_length / 2); // Break string into 2 parts
$str_chunks = chunk_split($string, $parts);
break;
default:
$parts = ceil($string_length / 3); // Break string into 3 parts
$str_chunks = chunk_split($string, $parts);
break;
}
$string_array = array_filter(explode(PHP_EOL, $str_chunks));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment