Last active
February 3, 2022 13:53
-
-
Save akrez/658b64924e79bfbcae24c6445a3a5999 to your computer and use it in GitHub Desktop.
#php #helper
This file contains hidden or 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 | |
//multiple delimiters in explode without using regex | |
function iexplode($delimiters, $string, $limit = PHP_INT_MAX) | |
{ | |
if (!is_array($delimiters)) | |
{ | |
$delimiters = [$delimiters]; | |
} | |
$del = reset($delimiters); | |
// | |
$result = []; | |
$maskedString = $string; | |
while (count($result) + 1 < $limit) | |
{ | |
$c = 1; | |
$maskedStringExploded = str_replace($delimiters, $del, $maskedString, $c); | |
$maskedStringExploded = explode($del, $maskedStringExploded, 2); | |
if (count($maskedStringExploded) == 2) | |
{ | |
$result[] = $maskedStringExploded[0]; | |
$maskedString = $maskedStringExploded[1]; | |
} | |
else | |
{ | |
$maskedString = $maskedStringExploded[0]; | |
break; | |
} | |
} | |
$result[] = $maskedString; | |
return $result; | |
} | |
public static function filterArray($arr, $doFilter = true, $checkUnique = true, $doTrim = true) | |
{ | |
if ($doTrim) { | |
$arr = array_map('trim', $arr); | |
} | |
if ($checkUnique) { | |
$arr = array_unique($arr); | |
} | |
if ($doFilter) { | |
$arr = array_filter($arr, 'strlen'); | |
} | |
return $arr; | |
} | |
public static function templatedArray($template = [], $values = [], $const = []) | |
{ | |
return $const + array_intersect_key($values, $template) + $template; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment