SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    
    $title = 'The Mummy Returns';
    
    preg_match('#^(?:the |an? )?([a-z])#i', $title, $parts);
    
    print_r($parts);
    
    ?>

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To expand on joebert's code, you probably want to include numeric titles, right?

    Code PHP:
    <?php
    $title = '10 Things I Hate About you';
     
    preg_match('#^(?:the |a |an )?([a-z0-9])#i', $title, $parts);
    $firstLetter = $parts[1];
    echo $firstLetter;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •