Simple question, what’s the best way to exclude words such as ‘a’ and ‘the’ at the beginning of an album title to better sort your array of titles alphabetically. I have a function that works but it seems to be kind of tacky, I was wondering if there is a better way to do it than this (I’m sure there is) that I’m not thinking of.
function cmp($a, $b) {
$excludes = array('a', 'the'); // Add excluded words here
foreach ($excludes as $word):
if (strtolower(substr($a['title'], 0, strlen($word) + 1)) == "{$word} ") $a['title'] = substr($a['title'], strlen($word) + 1);
if (strtolower(substr($b['title'], 0, strlen($word) + 1)) == "{$word} ") $b['title'] = substr($b['title'], strlen($word) + 1);
endforeach;
return strcasecmp($a['title'], $b['title']);
}
As stated, this works perfectly fine, it just doesn’t seem to be a very good way of doing it. Any ideas?
you could use
preg_replaceto simplify your code a bit: