SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    replace the 1st letter which is lower case to upper case

    Code:
    code
    $myVar="abCd";
    
    $myVar1 = str_replace('a','A', $myVar)
    
    echo $myVar1;
    
    result
    
    AbCd
    The code above replaces the first letter of $myVar which is small letter "a" to the big letter "A".
    The result is what I wanted when I knew the first letter is "a".
    But How can I change it when I don't know it?


    If it is "a", I like to change it "A".
    If it is "b", I like to change it "B".
    If it is "c", I like to change it "C".
    If it is "d", I like to change it "D".

    : :


    How can I replace the first letter of $myVar to the big letter?

  2. #2
    SitePoint Mentor silver trophybronze trophy
    Mikl's Avatar
    Join Date
    Dec 2011
    Location
    Edinburgh, Scotland
    Posts
    1,600
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    PHP has a built-in function to do this: ucfirst(). Pass it a string, and it returns the same string with the first letter capitalised.

    Mike

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I googled 'php replace the 1st letter which is lower case to upper case', and guess what appeared in position #4? The php manual with the function Mikl mentioned...

    Please do your own research first.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I am not positive, but I think the OP wants to respect the existing UC letters, ie convert:

    PHP Code:
    $myVar="abCd";

    // to 

    $myVar="AbCd"
    Can you confirm @dotJoon ; ?

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Cups View Post
    I am not positive, but I think the OP wants to respect the existing UC letters, ie convert:

    PHP Code:
    $myVar="abCd";

    // to 

    $myVar="AbCd"
    Doesn't ucfirst do exactly that?

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Doesn't ucfirst do exactly that?
    Based on the manual, it seems it would.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Well, I take that back, now I never knew that. Crikey, I wonder where I have assumed that in my code

  8. #8
    SitePoint Mentor silver trophybronze trophy
    Mikl's Avatar
    Join Date
    Dec 2011
    Location
    Edinburgh, Scotland
    Posts
    1,600
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    I am not positive, but I think the OP wants to respect the existing UC letters
    That's what I assumed. And, as Guido says, that's what ucfirst() does.

    If the function made the first letter upper case and the remaining letters lower-case, I'd expect it be called proper(). As far as I know, PHP doesn't have one of those.

    Mike

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    what about ucfirst().....?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  10. #10
    SitePoint Mentor silver trophybronze trophy
    Mikl's Avatar
    Join Date
    Dec 2011
    Location
    Edinburgh, Scotland
    Posts
    1,600
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    what about ucfirst().....?
    Your point being?

    Mike

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Off Topic:

    lol guess I have been here longer than I thought! Does no one remember the trim() parody thread?!!!!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Very interesting thread! ucfirst, ucfirst, ucfirst!

    To be on topic I'll add that ucfirst will not work with unicode character sets and if that is required then using mb functions is the way to go:
    PHP Code:
    function mb_ucfirst($s) {
      if (
    strlen($s) > 0) {
        return 
    mb_strtoupper(mb_substr($s01)) . mb_substr($s1);
      } else {
        return 
    '';
      }


  13. #13
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    PHP Code:
    $myVar="abCd";

    // to 

    $myVar="AbCd"
    Can you confirm @dotJoon ; ?
    Yes, it, the function ucfirst(), works fine. Thank you, Cups, as always.

  14. #14
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Well I accept your thanks most graciously, but I have to say others have provided the answer.

    I merely proved (again) to myself and others, that it is by trying to be a good member and stick your neck out and help others you expose yourself to correction.

    When that happens I learn from it.

    It can be humbling, but really, if there are any other passive readers and not participators out there, stick your neck out and jump in and provide your answers when you see the opportunity.

    You will get things wrong and it might make you feel a bit daft for not knowing something but you will learn a lot, you'll be helping others and you will make more friends.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •