Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    CA, USA
    Posts
    271
    Rep Power
    11

    Capitalization issues


    Does anyone know if there is a place where this code might be so we don't have to rewrite it all the time? I am sure it is quite common.

    From my form fields I have figured out the regular issues doing this.
    PHP Code:
    ucwords(strtolower(trim($_POST['firstname']))) 
    and that works wonders for most things but there probably will be some MacDonalds or McDonalds or O'Neils or the like. I even saw some LaRue and DuPois. for last names. And then with hyphenated names that throws another wrench into it.
    Is there a list of the most common names and the code to correct it? I think there should be somewhere.

    Also for some first names, people have started using initials like M.J. or AJ and my code makes is Mj or Aj.

    And lastly for the address. For PO Boxes only. I want the output to be PO Box 1234 and it always ends up Po Box 1234. And if they enter P.O. Box is goes to P.o. Box. I would like to remove the periods too if possible. I assume this can be done with preg_replace but I am not that familiar on how to make that work properly.

    Are there any others that people have run into and found fixes?
  2. #2
  3. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    The number of different ways of capitalising names is probably about the same as the number of last names so white listing is not going to work.

    Why are you doing strtolower anyway?
  4. #3
  5. I love your chinese eyes :*
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2006
    Location
    Her heart... she claims!
    Posts
    1,668
    Rep Power
    1049
    Why are you doing strtolower anyway?
    because if the complete value is already in capital so it will first change it all to lower case and then capitalize the first character of each word..
  6. #4
  7. I love your chinese eyes :*
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2006
    Location
    Her heart... she claims!
    Posts
    1,668
    Rep Power
    1049
    I am not sure if there is any ready made list of such names however you can build one for your and then keep the values in a table or an array and use a function like in_array() to check if the name exists in your special names list and if it does then replace it with the array value to preserve the correct form of the word....

    however there could be lots of such names and if you have a table of 10000 records it will hurt your server resources to check the name every time on each page


    however, if you already have names in a table then you can use the structure from the table because that must be correct as users entered their selves... whenever a user logs in store his name from table in a session and then display it on each page, so it will keep its original format...

    complex regex could be the ultimate solution too for both names and address format...
  8. #5
  9. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Why bother? what right do you have to "fix" how someone's decided how to enter their name? If they want all caps in their surname, let them.
  10. #6
  11. I love your chinese eyes :*
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2006
    Location
    Her heart... she claims!
    Posts
    1,668
    Rep Power
    1049
    Originally Posted by SimonGreenhill
    Why bother? what right do you have to "fix" how someone's decided how to enter their name? If they want all caps in their surname, let them.
    you are right up to some extent... however sometimes users fill forms etc in hurry and don't care about the proper format... and when you have to print formal reports and ledgers based on such data you can't afford to show crap on it and ultimately you have to format the text and values to make them presentable.. it is not only for printing purposes but also on websites..

    so in such cases these functions and tricks are very handy... because at the end of the day you have to keep your data / site neat, clean and presentable...
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Posts
    791
    Rep Power
    237
    Originally Posted by dandcp
    there probably will be some MacDonalds or McDonalds or O'Neils or the like. I even saw some LaRue and DuPois. for last names. And then with hyphenated names that throws another wrench into it.
    Just an idea: please decide for yourself if it is worth using in your system or not. The names like McDonalds or O'Neils have just 2 capital letters, other letters are in lower case ... Probably you could calculate the distance between the source name and name converted to lower case (please see the function [PHPNET="levenshtein"]levenshtein()[/PHPNET]). Then if the distance is bigger than 2, make your ucfirst(strtolwer()). It is just an idea though and it will not work in all cases you could possibly have with names ... Maybe you need something more complicated after all ...
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    CA, USA
    Posts
    271
    Rep Power
    11
    Originally Posted by jojoba
    you are right up to some extent... however sometimes users fill forms etc in hurry and don't care about the proper format... and when you have to print formal reports and ledgers based on such data you can't afford to show crap on it and ultimately you have to format the text and values to make them presentable.. it is not only for printing purposes but also on websites..

    so in such cases these functions and tricks are very handy... because at the end of the day you have to keep your data / site neat, clean and presentable...
    I have thought of that too. But I think I tend to agree with jojoba as there has been too many times when people forget that caps lock is on or they are just typing too quickly. I need it to look nice or correct.
    I think I will give them an option on page2 before submiting to edit their info incase it doesn't look quite right.
    But I still need help with some of the regular ones. like McDonald and O'Neil. Can someone please help on the best way to do this? And also the PO Box one too, please.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    CA, USA
    Posts
    271
    Rep Power
    11

    I am getting closer. Need a bit more advice/help please.


    ok so for the address field I think this will work for correcting the PO Box address to what I am wanting.
    PHP Code:
    $address ucwords(strtolower(trim($_POST['address'])));
    $patterns[0] = '/P.o./';
    $patterns[1] = '/Po/';
    $replacements[0] = 'PO';
    $address preg_replace($patterns$replacements$address); 
    Does that make sense?

    Can someone else please give me some more advice on the last name issue I am having?

    A few things.
    1. Hyphened last names. Would like result to be Smith-Johnson. (so need to remove any white spaces, and then strtolower and then ucfirst but need to figure out how to Capitalize the first letter after the hyphen.) I just don't understand reg expressions that well yet. Please help.

    2.Could someone help me with the McDonald and O'Neil capitalization issue. Is this the best way to do it?
    PHP Code:
    $lastname ucwords(strtolower(trim($_POST['lastname'])));
    $patterns[0] = '/macd/';
    $patterns[1] = '/mcd/';
    $patterns[2] = '/o\'n/';
    $replacements[0] = 'MacD';
    $replacements[1] = 'McD';
    $replacements[2] = 'O\'N';
    $lastname preg_replace($patterns$replacements$lastname); 
    Thanks again!
  18. #10
  19. Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jun 2005
    Posts
    5,964
    Rep Power
    4851
    I think you're wasting your time on McDonald, MacDonald, and O'Neil. You're solving three problems from a huge domain. Even if you used a relatively large look-up table, you wouldn't solve all your problems, but you'd do better than three. You've identified an issue. Now you have to face facts.

    The hyphenation issue should be relatively simple. Roll your own UC-after-hyphen, and don't apply it to text in general.
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    You're going to have to do all letters after "Mac", for names like "MacAdams" or "MacWilliam". Of course, that means that you will also have to NOT do last names like "Macintosh." I think your best bet is to just let the user determine what their name looks like. Even if you can find all instances of European capitalization, you're just going to offend people with African or Asian names. Just let the user decide.

    If that fails, I remember there used to be a perl library that would do this, because Perl programmers apparently have nothing better to do. Ask about it on the perl boards, maybe you could translate it into PHP.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    CA, USA
    Posts
    271
    Rep Power
    11
    Originally Posted by sizablegrin
    I think you're wasting your time on McDonald, MacDonald, and O'Neil. You're solving three problems from a huge domain. Even if you used a relatively large look-up table, you wouldn't solve all your problems, but you'd do better than three. You've identified an issue. Now you have to face facts.

    The hyphenation issue should be relatively simple. Roll your own UC-after-hyphen, and don't apply it to text in general.
    OK, sounds good. I will take your advice and just let it be for that MacDonald and other last names issue. Thanks to both of you.

    re the hyphen issue, it is not that simple to me. I still don't understand exactly. Can you give me an example of how to "Roll your own UC-after-hyphen"?

    Thank you muchly.
  24. #13
  25. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    PHP Code:
    $original_name "Smith-wilson";
    $uc_after_hyphen preg_replace("#^(.+-)(.)(.+)$#e""'\\1' . strtoupper('\\2') . '\\3'"$original_name);
    echo 
    $uc_after_hyphen
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    CA, USA
    Posts
    271
    Rep Power
    11
    Originally Posted by ManiacDan
    PHP Code:
    $original_name "Smith-wilson";
    $uc_after_hyphen preg_replace("#^(.+-)(.)(.+)$#e""'\\1' . strtoupper('\\2') . '\\3'"$original_name);
    echo 
    $uc_after_hyphen
    -Dan
    Yep that's it! That's awesome! Thank you so much Dan.

    I don't understand exactly how it works but it does work. I want to learn how to do this myself.

    Would you be willing give me a little lesson or explain how this works or what it all does? I would appreciate it.
    PHP Code:
    ("#^(.+-)(.)(.+)$#e""'\\1' . strtoupper('\\2') . '\\3'"$original_name); 
    Thanks again
  28. #15
  29. Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jun 2005
    Posts
    5,964
    Rep Power
    4851
    Regex works, but it's considerably less efficient. A lot of people who never wrote a regex library don't really appreciate that. The straightforward approach is:
    Code:
    Do while characters remain:
       if find hyphen
          if find non-hyphen
             replace with upper-case
    end
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo