Created
August 19, 2020 20:21
-
-
Save ahmu83/417c3874e797ba35a2b23434885f66af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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