php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #78393 Incorrectly specified behavior
Submitted: 2019-08-09 14:53 UTC Modified: 2020-03-15 22:27 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dmorgan at adobe dot com Assigned:
Status: Open Package: Strings related
PHP Version: Irrelevant OS: n/a
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
29 - 7 = ?
Subscribe to this entry?

 
 [2019-08-09 14:53 UTC] dmorgan at adobe dot com
Description:
------------
The documentation describes the operation of the method as follows:
"Returns a string with the first character of each word in str capitalized, if that character is alphabetic."

The documentation describes a word as follows:

"The definition of a word is any string of characters that is immediately after any character listed in the delimiters parameter (By default these are: space, form-feed, newline, carriage return, horizontal tab, and vertical tab)."

It should probably read more like:
"The definition of a word is any string of characters that is EITHER AT THE START OF STR, OR immediately after any character listed in the delimiters parameter (By default these are: space, form-feed, newline, carriage return, horizontal tab, and vertical tab)."

Alternatively, the first line could be changed to read something like:
"Returns a string with the first character of STR, AND THE FIRST CHARACTER OF each SUBSEQUENT word in str capitalized, if that character is alphabetic."

I would recommend, while you're in there, clarifying the first line by adding something like "All characters in the remainder of word are left unchanged." - many are caught out by interpreting this as a TitleCase method, so try to use this function to Title Case an UPPER CASE string, without first lowercasing it. You can see at least one such victim in the comments on the page.

Finally, I'd also recommend rigorously clarifying what's meant by "alphabetic", and how that definition might change with localization; and/or giving a clear pointer to a page where such a clarification is made (not just in the "see also" section!)

Test script:
---------------
php -r 'echo ucwords("foo_bar_BAZ", "_") . "\n";

Expected result:
----------------
If a word were indeed "any string of characters that is immediately after any character listed in the delimiters parameter", you'd expect the output of the above to be "foo_Bar_BAZ", or possibly "foo_Bar_Baz" without the optional clarification.

Actual result:
--------------
The output is instead "Foo_Bar_BAZ".

Note how the first character of the string is capitalized, even though it is NOT immediately after the delimiter '_' as stated in the documentation.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-15 22:27 UTC] cmb@php.net
-Summary: Incorrectly specified behavior. +Summary: Incorrectly specified behavior -Package: PHP Language Specification +Package: PDF related
 [2020-03-15 22:27 UTC] cmb@php.net
-Package: PDF related +Package: Strings related
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Nov 25 20:01:23 2020 UTC