Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:34:36+00:00 2026-06-09T09:34:36+00:00

I am coding a tag system for a custom CMS built with Codeigniter and

  • 0

I am coding a tag system for a custom CMS built with Codeigniter and I’m trying to enforce a particular format.

Basically, I need the first letter of each word to be capitalized with the exception of the following, which should be lowercase:

  • Articles: a, an, the
  • Coordinating Conjunctions: and, but, or, for, nor, etc.
  • Prepositions (fewer than five letters): with, on, at, to, from, by, etc.

Furthermore, if the tag starts with one of the above, it should be capitalized.

Some examples of properly formatted tags:

  • Game of Thrones
  • Of Mice and Men
  • From First to Last
  • Lord of the Rings
  • Need for Speed

So far I just have:

$tag = 'Lord of the Rings';
$tag = ucwords($tag); 

$patterns = array('/A/', '/An/', '/The/', '/And/', '/Of/', '/But/', '/Or/', '/For/', '/Nor/', '/With/', '/On/', '/At/', '/To/', '/From/', '/By/' );
$lowercase = array('a', 'an', 'the', 'and', 'of', 'but', 'or', 'for', 'nor', 'with', 'on', 'at', 'to', 'from', 'by' );

$formatted_tag = preg_replace($patterns, $lowercase, $tag);

// capitalize first letter of string
$formatted_tag = ucfirst($formatted_tag);

echo $formatted_tag;

This produces the correct result of Lord of the Rings, but how can I avoid duplicating the arrays? It’s tedious matching them up when I add new words.

I’m sure there are some words that should be included that I’m missing, are there any existing functions or classes that I can use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:34:37+00:00Added an answer on June 9, 2026 at 9:34 am

    You don’t need the $lowercase array if you use a custom callback with preg_replace_callback(). Also, your current method needs word boundaries, otherwise it’ll replace Android with android or bAnd with band. Finally, creating N number of regexes for N words is inefficient and not necessary, as this can be done with one single regex.

    I would just keep a words array:

    $words = array('A', 'An', 'The', 'And', 'Of', 'But', 'Or', 'For', 'Nor', 'With', 'On', 'At', 'To', 'From', 'By' );
    

    And create one dynamic regex, complete with word boundaries, like so:

    $regex = '/\b(' . implode( '|', $words) . ')\b/i';
    

    And now replace all of the matches with their lowercase counterpart:

    $formatted_tag = preg_replace_callback( $regex, function( $matches) {
        return strtolower( $matches[1]);
    }, $tag);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to override the DateTime template since I need custom class names
When coding a tag file, how to instruct caller to skip rest of the
How can we write html tidy coding only for inserting closing tag in the
Label tag coding style. Can I write my label tag like this. <input type=text
I'm coding a function in C++ to load a MAT-File (level 5) format based
I am trying to tag friends in facebook video, using Facebook API, I am
Working with a struts 1 project, I'm trying to save myself hours of coding
I'm trying to use regular expressions to remove certain blocks of coding from a
When coding in C# I have always found the tag remarks very useful for
I'm trying to see if there is a way to split a CFSAVECONTENT tag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.