# ucwords() vs title case

- Thread: https://externals.io/message/75140
- Original archive: https://news-web.php.net/php.internals/75140
- Started by Tjerk Meesters on 2014-06-30T11:54:58+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #75140 — Tjerk Meesters — 2014-06-30T11:54:58+00:00

URL: https://externals.io/message/75140#75140

Hi internals,

I came across this old bug: https://bugs.php.net/bug.php?id=34407

The desired behaviour for that particular person:

    "the (world) now" => "The (World) Now"

Currently PHP adopts a very simple rule:
1. Capitalise first character (no matter what it is)
2. Capitalise character preceded by a space, tab, etc.

Using string.title() from Python you'd get the expected behaviour; they use:
1. Capitalise first letter of a word
2. Lowercase subsequent letters of a word
3. Non-letters delimit words

Personally I find that the latter is too much of a departure from what we
currently have; a compromise could be to treat punctuation as a word
delimiter.

Thoughts?


-- 
--
Tjerk

---

## #75141 — Andrea Faulds — 2014-06-30T11:57:37+00:00

URL: https://externals.io/message/75140#75141 · In reply to: #75140

> Hi internals,
> 
> I came across this old bug: https://bugs.php.net/bug.php?id=34407
> 
> The desired behaviour for that particular person:
> 
>    "the (world) now" => "The (World) Now"
> 
> Currently PHP adopts a very simple rule:
> 1. Capitalise first character (no matter what it is)
> 2. Capitalise character preceded by a space, tab, etc.
> 
> Using string.title() from Python you'd get the expected behaviour; they use:
> 1. Capitalise first letter of a word
> 2. Lowercase subsequent letters of a word
> 3. Non-letters delimit words
> 
> Personally I find that the latter is too much of a departure from what we
> currently have; a compromise could be to treat punctuation as a word
> delimiter.

Hmm. Why not make it follow what \b in a regex would do, looking for “word boundaries”?
--
Andrea Faulds
http://ajf.me/

---

## #75143 — Sebastian Krebs — 2014-06-30T12:36:29+00:00

URL: https://externals.io/message/75140#75143 · In reply to: #75140

2014-06-30 13:54 GMT+02:00 Tjerk Meesters <tjerk.meesters@gmail.com>:

> Hi internals,
>
> I came across this old bug: https://bugs.php.net/bug.php?id=34407
>
> The desired behaviour for that particular person:
>
>     "the (world) now" => "The (World) Now"
>
> Currently PHP adopts a very simple rule:
> 1. Capitalise first character (no matter what it is)
> 2. Capitalise character preceded by a space, tab, etc.
>
> Using string.title() from Python you'd get the expected behaviour; they
> use:
> 1. Capitalise first letter of a word
> 2. Lowercase subsequent letters of a word
>

Actually letting a function "uppercase first (letter)" lowercase all
subsequent would be even more unexpected.


> 3. Non-letters delimit words
>
> Personally I find that the latter is too much of a departure from what we
> currently have; a compromise could be to treat punctuation as a word
> delimiter.
>
> Thoughts?
>
>
> --
> --
> Tjerk
>



-- 
github.com/KingCrunch

---

## #75157 — Kris Craig — 2014-06-30T23:25:49+00:00

URL: https://externals.io/message/75140#75157

On Mon, Jun 30, 2014 at 5:33 AM, Rowan Collins <rowan.collins@gmail.com>
wrote:

> Andrea Faulds wrote (on 30/06/2014):
>
> > […]
>
> Unfortunately, the cleverer you try to be, the more edge cases you find.
> For instance, using \b will capitalise the 's' after an apostrophe, e.g. in
> "Andrea'S Suggestion".
>
> The function we have in our code base at the moment looks like this:
>
> function smart_uc_words($string)
> {
>         $string = strtolower(trim($string));
>         // Capitalise any word char preceded by a non-word char other than
> an apostrophe
>         $string = preg_replace_callback('/(?<!\w|\')(\w)/', function($m){
> return strtoupper($m[1]); }, $string);
>         // Capitalise any word char which comes between an apostrophe and
> another word char
>         $string = preg_replace_callback('/(?<=\')(\w)(?=\w)/',
> function($m){ return strtoupper($m[1]); }, $string);
>
>         return $string;
> }
>

What about leaving the default behavior as-is but adding an optional
argument to specify how to determine these boundaries?  So if you did
something like ucwords( "hello, world!", '\b' ) or ucwords( "hello,
world!", array( ' ', '.', ... ) ), the user could control the behavior
while existing ucwords( $arg ) code would behave as it does now without any
BC.

--Kris

---

## #75163 — Tjerk Meesters — 2014-07-01T17:19:20+00:00

URL: https://externals.io/message/75140#75163 · In reply to: #75157

Hi Kris,




> On Mon, Jun 30, 2014 at 5:33 AM, Rowan Collins <rowan.collins@gmail.com>
> wrote:
>
> > […]
> we
> > […]
> in
> > […]
> than
> > […]
>
> What about leaving the default behavior as-is but adding an optional
> argument to specify how to determine these boundaries?  So if you did
> something like ucwords( "hello, world!", '\b' ) or ucwords( "hello,
> world!", array( ' ', '.', ... ) ), the user could control the behavior
> while existing ucwords( $arg ) code would behave as it does now without any
> BC.
>

Yeah, that seems like an option, so basically how `trim()` works too; treat
these characters as word boundaries (default is " \t\r\n").

    ucwords("hello (new) world", " ()");

I'll prepare a PR for this and see how far that takes us :) let me know if
you guys have any other ideas.



> --Kris
>



-- 
--
Tjerk

---

## #75230 — Tjerk Meesters — 2014-07-03T12:39:02+00:00

URL: https://externals.io/message/75140#75230 · In reply to: #75163

On Wed, Jul 2, 2014 at 1:19 AM, Tjerk Meesters <tjerk.meesters@gmail.com>
wrote:

> Hi Kris,
>
>
> 
>
> > […]
>
> Yeah, that seems like an option, so basically how `trim()` works too;
> treat these characters as word boundaries (default is " \t\r\n").
>
>     ucwords("hello (new) world", " ()");
>
> I'll prepare a PR for this and see how far that takes us :) let me know if
> you guys have any other ideas.
>

I've created a PR here: https://github.com/php/php-src/pull/706

If there are no objections I would like to commit this into 5.4 onwards
somewhere next week.

Thanks.


>
>
>
> > […]
>
>
>
> --
> --
> Tjerk
>



-- 
--
Tjerk

---

## #75231 — Peter Cowburn — 2014-07-03T12:56:08+00:00

URL: https://externals.io/message/75140#75231 · In reply to: #75230

> On Wed, Jul 2, 2014 at 1:19 AM, Tjerk Meesters <tjerk.meesters@gmail.com>
> wrote:
>
> > […]
> find.
> > […]
> if
> > […]
>
> I've created a PR here: https://github.com/php/php-src/pull/706


Your previous mail mentioned, "so basically how `trim()` works too", but
the PR doesn't quite do that.

Should `ucwords()` also accept character ranges, just like `trim()`?  i.e.,
ucwords("Foo bar", "a..z");  [not a very practical example, I know]


>
>
> If there are no objections I would like to commit this into 5.4 onwards
> somewhere next week.
>
> Thanks.
>
>
> > […]
>
>
>
> --
> --
> Tjerk

---

## #75232 — Tjerk Meesters — 2014-07-03T13:15:41+00:00

URL: https://externals.io/message/75140#75232 · In reply to: #75231

Hi!


On Thu, Jul 3, 2014 at 8:56 PM, Peter Cowburn <petercowburn@gmail.com>
wrote:

>
>
>
> 
>
> > […]
>
>
> Your previous mail mentioned, "so basically how `trim()` works too", but
> the PR doesn't quite do that.
>

That's somewhat embarrassing; I didn't realise that character ranges are
supported in `trim()` =S

Despite this oversight, I personally don't see a practical need in
supporting a character range because the given characters are not likely to
be letters, but rather hyphens, braces, punctuation marks, spaces, etc. I
was also hoping to keep the function rather simple :)


>
> Should `ucwords()` also accept character ranges, just like `trim()`?  i.e.,
> ucwords("Foo bar", "a..z");  [not a very practical example, I know]
>
>
> > […]
>
>


-- 
--
Tjerk

---

## #75233 — Peter Cowburn — 2014-07-03T13:45:30+00:00

URL: https://externals.io/message/75140#75233 · In reply to: #75232

> Hi!
>
>
> On Thu, Jul 3, 2014 at 8:56 PM, Peter Cowburn <petercowburn@gmail.com>
> wrote:
>
> > […]
>
> That's somewhat embarrassing; I didn't realise that character ranges are
> supported in `trim()` =S
>
> Despite this oversight, I personally don't see a practical need in
> supporting a character range because the given characters are not likely to
> be letters, but rather hyphens, braces, punctuation marks, spaces, etc. I
> was also hoping to keep the function rather simple :)
>

The charmasks aren't limited to letters only.  You could go crazy and use
"\0../;..@[..`{..\x7F" for everything non-alphanumeric in ASCII, if you
really wanted to.

That said, I have no particular preference one way or the other (for
 `ucwords()`) and was mostly just clarifying the point about working how
 `trim()` works, or not.


>
>
> > […]
>
>
> --
> --
> Tjerk
