[], 'female'=>[]]; }, $list); foreach (scandir(__DIR__ . DIRECTORY_SEPARATOR . 'layers') as $file) { $layer = self::findLayer($file); if ($layer) { if (self::isForMale($file)) $list[$layer]['male'][] = $file; if (self::isForFemale($file)) $list[$layer]['female'][] = $file; } } return $list; } /** * determine the layer name from a file name * @param $fileName * @return null|string */ private static function findLayer($fileName) { $found = ''; foreach (self::$layers as $layer) { if (substr_compare($layer, $fileName, 0, strlen($layer), true) == 0) { if (strlen($layer) > strlen($found)) $found = $layer; } } return $found == '' ? null : $found; } /** * determine if the file is meant for male avatars * @param $fileName * @return bool */ private static function isForMale($fileName) { return (preg_match('/_f?mf?\.png/', $fileName) > 0 || preg_match('/_[mf]+\.png/', $fileName) == 0); } /** * determine if the file is meant for female avatars * @param $fileName * @return bool */ private static function isForFemale($fileName) { return (preg_match('/_m?fm?\.png/', $fileName) > 0 || preg_match('/_[mf]+\.png/', $fileName) == 0); } }