Page 1 of 1
Can you set the casing but ignore non titles?
Posted: Fri Mar 18, 2016 12:44 pm
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??
Re: Can you set the casing but ignore non titles?
Posted: Fri Mar 18, 2016 12:58 pm
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.
Re: Can you set the casing but ignore non titles?
Posted: Fri Mar 18, 2016 6:27 pm
by simonmlewis
Think I might change the system and use slugs from the database, to manage the urls.
Re: Can you set the casing but ignore non titles?
Posted: Fri Mar 18, 2016 11:11 pm
by requinix
That's a very good idea.