I want to standardize a user-supplied string. I'd like the first letter to be capitalized for the name and if they have entered two last names, then capitalize the first and second names. For example, if someone enters:
marriedname maidenname
It would convert this to Marriedname Maidenname
and so on if there is more than two names.
The other scenario is when someone has an apostrophe in their name. If someone enters:
o'connell
This would need to convert to O'Connell
.
I was using:
ucfirst(strtolower($last_name));
However, as you can tell that wouldn't work for all the scenarios.
This will capitalize all word's first letters, and letters immediately after an apostrophe. It will make all other letters lowercase. It should work for you:
str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($last_name))));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With