Can you set the casing but ignore non titles?

HTML, CSS and anything else that deals with client side capabilities.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you set the casing but ignore non titles?

Post by simonmlewis »

Food And Beverage
Uk Transport

These are terms that Capitalized text-transform is messing up for us.

We force the insert for the name to be lowercase, so it's shows lowercase in the URL.
We don't use slugs. So if they add "my web site" as the title, then the url is "my-web-site".

Is there a text transform that identifies the words someshow, so it knows UK is UK and not Uk, and the same for And If On Was... etc??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can you set the casing but ignore non titles?

Post by requinix »

Not built-in. Consider "ME": how would anything know whether that's the pronoun or the notoriously buggy edition of Windows?

Rather than lowercase and re-capitalize words, how about specifically capitalizing lowercase words and specifically lowercasing some capitalized words? Confusing?

Lowercasing and then capitalizing:

Code: Select all

// do some general cleanup on the string first
$input = trim("UK Transport");

// lowercase everything and lose all the capitalization in "UK"
$output = strtolower($input);
// only regain the first letter of it
$output = ucwords($output);

echo $output;
Capitalizing and then lowercasing:

Code: Select all

// do some general cleanup on the string first
$input = trim("UK Transport");

// keep all existing capitalization and "fix" something that had a leading lowercase letter
$output = preg_replace_callback('/(^|\s)[a-z]/', function($match) { return strtoupper($match[0]); }, $input);
// "fix" (or perhaps un-fix from the previous step) a known common word with a leading uppercase letter
$output = preg_replace_callback('/(\s)(and|if|on|was|...)(\s|$)/', function($match) { return strtolower($match[0]); }, $output);

echo $output;
1. Note that those regexes are using a space to indicate the beginning or end of a word.
2. Note that the second regex will deliberately not lowercase a common word if it's at the beginning of the string.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you set the casing but ignore non titles?

Post by simonmlewis »

Think I might change the system and use slugs from the database, to manage the urls.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can you set the casing but ignore non titles?

Post by requinix »

That's a very good idea.
Post Reply