Jump to content

Remove "The" and "A"


happyturtle

Recommended Posts

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

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

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

// 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.