happyturtle Posted June 20, 2009 Share Posted June 20, 2009 When I sort the results on my website it shows all of the results that have "The" under "T" and "A" under "A". For example I want the results to show like this: The Simpsons would show up under "S" and not "T". If anyone can help it would be must appreciated. Link to comment https://forums.phpfreaks.com/topic/163040-remove-the-and-a/ Share on other sites More sharing options...
fnairb Posted June 20, 2009 Share Posted June 20, 2009 How about ... <?php $titles = array("The Simpsons", "A Brave New World", "Then and Now"); foreach ($titles as $title) { print "$title --> "; if (preg_match('/^(The|A)\b\s+(.+)/',$title, $keep)) { $title = $keep[2] . ', ' . $keep[1]; } print "$title\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/163040-remove-the-and-a/#findComment-860280 Share on other sites More sharing options...
happyturtle Posted June 20, 2009 Author Share Posted June 20, 2009 I am getting the information for a mysql database. There are 100s of entries. Link to comment https://forums.phpfreaks.com/topic/163040-remove-the-and-a/#findComment-860286 Share on other sites More sharing options...
fnairb Posted June 20, 2009 Share Posted June 20, 2009 Gotcha. One way to solve it would be to add another column (SORTABLE_TITLE or DISPLAY_TITLE). You could then write a quick script to run through the entries to populate this new column with a more friendly string. Another option would be to run through the records records before you use them to create a more friendly title. Then sort those results. Then use that. Kinda heavy handed but it would work. Link to comment https://forums.phpfreaks.com/topic/163040-remove-the-and-a/#findComment-860291 Share on other sites More sharing options...
.josh Posted June 20, 2009 Share Posted June 20, 2009 or combine a sortby and regex in your query. or you can go through and rewrite all of the titles to put it on the end like so: Simpsons, The It's acceptable to leave it like that, displaying it, or if you want to nonetheless display it as "The Simpsons" you can have a condition in your display loop to look for it and reorder it. Link to comment https://forums.phpfreaks.com/topic/163040-remove-the-and-a/#findComment-860303 Share on other sites More sharing options...
Andy-H Posted June 20, 2009 Share Posted June 20, 2009 // fetch data $words = array('a', 'the'); $title = explode(" ", $title); if ( in_array(strToLower($title[0]), $words) ) { for ( $i = 1; $i < count($title) - 1; $i++ ) { echo $title[$i] . ' '; } echo ',' . $title[0]; } else { echo implode(' ', $title); } could do something like that? Link to comment https://forums.phpfreaks.com/topic/163040-remove-the-and-a/#findComment-860310 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.