Manuel Canga
Posted on February 27, 2023
Securely, you did some item list like this once, didn't you?:
<?php
function to_array(string $allowed_extensions): array {
$allowed_list = explode(',', $allowed_extensions);
return array_map(fn($ext)=> trim($ext), $allowed_list);
}
to_array('.pdf, .odt, .doc'); //['.pdf', '.odt', '.doc']
not bad, but this is better:
<?php
function to_array(string $allowed_extensions): array {
$allowed_list = str_word_count($allowed_extensions, format: 1, characters: '.');
return array_map(fn($ext)=> trim($ext), $allowed_list);
}
to_array('.pdf, .odt, .doc'); //['.pdf', '.odt', '.doc']
Why?, because now you can change list format without changing nothing more. Look!:
to_array('.pdf|.odt|.doc'); //['.pdf', '.odt', '.doc']
to_array('.pdf;.odt;.doc'); //['.pdf', '.odt', '.doc']
Happy coding!
💖 💪 🙅 🚩
Manuel Canga
Posted on February 27, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.