How do I display the first letter as uppercase?

后端 未结 7 1343
孤城傲影
孤城傲影 2021-01-18 08:41

I have fname and lname in my database, and a name could be stored as JOHN DOE or john DOE or JoHN dOE, but ultimately I want to display it as John Doe

fname being Jo

相关标签:
7条回答
  • 2021-01-18 09:04

    Be aware that there are other capitalisation formats, such as John McDoe, John D'Oe, John de O, and John van den Doe.

    0 讨论(0)
  • 2021-01-18 09:13

    I'm not sure where and how you want to display it, but if it's on a web-page you might try simply altering the display using CSS. Try adding:

    text-transform:capitalize;
    

    to the relevant rule, like this:

    .name { text-transform:capitalize;}
    

    if you put the name in a div or span with class="name". Of course, this will not in any way alter the database entry, but I wasn't clear which was preferable.

    0 讨论(0)
  • 2021-01-18 09:16

    Combining PHP shorthand functions strtolower and ucwords solves your problem:

    function ucname($f, $l)
    {
        return ucwords(strtolower($f." ".$l));
    }
    echo ucname($fname, $lname);
    

    On a side note, keep in mind that you can do that sort of data beautification at many different stages:

    1. before insertion, in your application
    2. during insertion, with string functions in the SQL insert/update query
    3. during extraction, with string functions in the SQL select query
    4. after extraction, in your application
    0 讨论(0)
  • 2021-01-18 09:18

    Change the names to lower and then add ('A' - 'a') to the first letter of fname & lname.

    0 讨论(0)
  • 2021-01-18 09:21

    For simple names, this will work. Beware of special cases (like "Ronald McDonald" in the below example).

    In SQL Server:

    SELECT  --step 2: combine broken parts into a final name
      NAME_PARTS.FNAME_INITIAL + NAME_PARTS.REST_OF_FNAME AS FNAME
     ,NAME_PARTS.LNAME_INITIAL + NAME_PARTS.REST_OF_LNAME AS LNAME
    FROM
      (     --step 1: break name into 1st letter and "everything else"
      SELECT
        UPPER(SUBSTRING(TEST.FNAME,1,1)) AS FNAME_INITIAL
       ,UPPER(SUBSTRING(TEST.LNAME,1,1)) AS LNAME_INITIAL
       ,LOWER(SUBSTRING(TEST.FNAME,2,LEN(TEST.FNAME))) AS REST_OF_FNAME
       ,LOWER(SUBSTRING(TEST.LNAME,2,LEN(TEST.LNAME))) AS REST_OF_LNAME
      FROM
        (   --step 0: generate some test data
              SELECT 'john' AS FNAME, 'doe' as LNAME
        UNION SELECT 'SUZY', 'SMITH'
        UNION SELECT 'bIlLy', 'BOb'
        UNION SELECT 'RoNALD', 'McDonald'
        UNION SELECT 'Edward', NULL
        UNION SELECT NULL, 'Jones'
        ) TEST
      ) NAME_PARTS
    

    In Oracle

    SELECT  --step 2: combine broken parts into a final name
      NAME_PARTS.FNAME_INITIAL || NAME_PARTS.REST_OF_FNAME AS FNAME
     ,NAME_PARTS.LNAME_INITIAL || NAME_PARTS.REST_OF_LNAME AS LNAME
    FROM
      (     --step 1: break name into 1st letter and "everything else"
      SELECT
        UPPER(SUBSTR(TEST.FNAME,1,1)) AS FNAME_INITIAL
       ,UPPER(SUBSTR(TEST.LNAME,1,1)) AS LNAME_INITIAL
       ,LOWER(SUBSTR(TEST.FNAME,2,LENGTH(TEST.FNAME))) AS REST_OF_FNAME
       ,LOWER(SUBSTR(TEST.LNAME,2,LENGTH(TEST.LNAME))) AS REST_OF_LNAME
      FROM
        (   --step 0: generate some test data
              SELECT 'john' AS FNAME, 'doe' as LNAME FROM DUAL
        UNION SELECT 'SUZY', 'SMITH' FROM DUAL
        UNION SELECT 'bIlLy', 'BOb' FROM DUAL
        UNION SELECT 'RoNALD', 'McDonald' FROM DUAL
        UNION SELECT 'Edward', NULL FROM DUAL
        UNION SELECT NULL, 'Jones' FROM DUAL
        ) TEST
      ) NAME_PARTS
    
    0 讨论(0)
  • 2021-01-18 09:22

    An example from php.net:

    $bar = 'HELLO WORLD!';
    $bar = ucfirst($bar);             // HELLO WORLD!
    $bar = ucfirst(strtolower($bar)); // Hello world!
    

    Bear in mind the notes about the locales though...

    0 讨论(0)
提交回复
热议问题