views:

63

answers:

1

I use the following in mysql to get titles in libray sort, e.g. The Godfather is sorted as Godfather.

SELECT apnumber, aptitle, IF( aptitle LIKE 'The %', SUBSTRING( aptitle, 5 ) , IF( aptitle LIKE 'A %', SUBSTRING( aptitle, 3 ) , IF( aptitle LIKE 'An %', SUBSTRING( aptitle, 4 ) , aptitle ) ) ) AS sorttitle etc...

What's the most efficient way in PHP to change a string into a library sorted version? That is dropping any "An ", "A " or "The " at the beginning of a title. Am I correct in assuming I need to be looking at something like:

substr_replace( "The " , "" , 0)

+1  A: 

I think you are generally correct, yes. As far as I'm aware, there is nothing built in to either PHP or MySQL to assist with this type of sorting. Code implementations I've seen have all used a set of substring replacement rules to get rid of unwanted prefixes, just as you are assuming above. Generally preg_replace() is used so that you can specify a match at the beginning of the string only:

preg_replace("/^The /",'',$title,1);

Something to consider if you're willing to forego a little disk space and normalization would be to store a second column called something like sort_title, and do the prefix removal before inserting the records. This would allow you to index and ORDER BY on the sort_title field within MySQL, and reduce the complexity of your SELECT statements.

As another aside, this would be a great extension to write for PHP!

zombat
Thanks...I actually started using the mysql statement above to populate some older tables...now on to a function to do it direct from PHP!
Ian
Does the search pattern in preg_replace allow OR'ing || ? Wondering if this can be done in just one preg_repalce line...
Ian
Yes, `preg_replace` uses normal regular expression syntax, so ORs are supported via a single pipe character. You can do something like `preg_replace("/^(The |A |An |)/",'',$title,1)`.
zombat