Titles: Remove A, An, The from beginning Title w/o MySql

Question,

I already know how to use mysql to create alphabetical titles by removing the opening “A” “An” “The” segments, but am now wondering how I can just do it to a single title in PHP.

I have a film opened to the page, with the entire film title as $filmtitle

I want to take the first letter from the $filmtitle, but not grab the first letter from The, An or A.

Like, I want it to return “A” for something like “Avatar” and “M” for something like “The Mummy Returns.”

How can I do this with PHP?

Cheers!
Ryan

<?php

$title = 'The Mummy Returns';

preg_match('#^(?:the |an? )?([a-z])#i', $title, $parts);

print_r($parts);

?>

one option is:

  1. put all the “bad words” like A, An, The etc in an array.

  2. explode() the title string into its individual words.

  3. loop through each of the “exploded” words and as soon as you hit the first exploded word that is not in the badwords array, extract its first letter.

To expand on joebert’s code, you probably want to include numeric titles, right?

<?php
$title = '10 Things I Hate About you';

preg_match('#^(?:the |a |an )?([a-z0-9])#i', $title, $parts);
$firstLetter = $parts[1];
echo $firstLetter;