SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Capitalizing First Letter Minus The, And, etc.

    Suppose I have the following in three database table cells:

    the golden eagle
    **** of the rock
    bird of paradise

    And suppose I'm using the following script to display them:

    PHP Code:
    <?=$r['Species'?>
    Is there a way to display them so that...

    1) The first letter of each word is capitalized...
    2) EXCEPT words like the, and and of are never capitalized...
    3) EXCEPT when they are the first words in a string

    So the above terms would be displayed like this:

    The Golden Eagle
    **** of the Rock
    Bird of Paradise

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Uk
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This was in the php blog a few weeks ago
    http://www.sitepoint.com/blog-post-view.php?id=243299

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vex
    This was in the php blog a few weeks ago
    http://www.sitepoint.com/blog-post-view.php?id=243299
    Thanks, that looks like a good tutorial. I'm a little confused, though. Suppose I want to apply this technique to the following database value:

    PHP Code:
    <?php $Symbol "The&nbsp;" $r['Symbol']; ?>
    Do I insert the upper-case script BEFORE or AFTER the above script...and how/where do I plug in $r['Symbol']? Thanks.

    PHP Code:
    <?php
    function strtotitle($title)
    // Converts $title to Title Case, and returns the result.
    {
      
    // Our array of 'small words' which shouldn't be capitalised if
      // they aren't the first word.  Add your own words to taste.
      
    $smallwordsarray = array(
        
    'of','a','the','and','an','or','nor','but','is','if','then','else','when',
        
    'at','from','by','on','off','for','in','out','over','to','into','with'
        
    );

      
    // Split the string into separate words
      
    $words explode(' '$title);

      foreach (
    $words as $key => $word)
      {
        
    // If this word is the first, or it's not one of our small words, capitalise it
        // with ucwords().
        
    if ($key == or !in_array($word$smallwordsarray))
          
    $words[$key] = ucwords($word);
      }

      
    // Join the words back into a string
      
    $newtitle implode(' '$words);

      return 
    $newtitle;
    }


    // CSS text-transform property
    ?>

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put the function either in the same file or in an included file and use it like:
    <?php $Symbol = "The&nbsp;" . strtotitle($r['Symbol']); ?>

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    Put the function either in the same file or in an included file and use it like:
    <?php $Symbol = "The&nbsp;" . strtotitle($r['Symbol']); ?>
    I see - it's even easier than I thought. Thanks for the help; that's a really cool script.


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
  •