SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Rusted & Weathered HumanClay's Avatar
    Join Date
    Sep 2000
    Location
    Milwaukee, WI
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stripping First Character?

    Lets say I have a string set to a variable:

    $string = "sitepoint";

    How would I extract the first letter of that string, capitalize it, and set it to another variable $letter so I end up with:

    $letter = "S";
    Steve Caponetto - [profile] [e-mail]
    CreedFeed.com - feed your need for Creed!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Try this:
    PHP Code:
    <?php
    $string 
    'sitepoint';
    $lower='abcdefghijklmnopqrstuvwxyz';
    $upper='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    for (
    $i=0$i26$i++)
    {
        if (
    $string[0]==$lower[$i])
        {
            
    $letter $upper[$i];
            break;
        }

    }
    ?>
    Last edited by 7stud; Jun 1, 2001 at 22:02.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh...


    PHP Code:
    $string "sitepoint"
    $letter strtoupper(substr($string,0,1)); 
    or


    PHP Code:
    $string "sitepoint"
    $letter substr(ucfirst($string),0,1); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol....darn it! I was looking for that function freddy, but I thought I got my java mixed up with my php.

  5. #5
    Rusted & Weathered HumanClay's Avatar
    Join Date
    Sep 2000
    Location
    Milwaukee, WI
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freddy, thanks.

    7stud, actually what you posted was also helpful. I'm still learning PHP slowly, so that code fragment is a good example of something that I would never have thought of. It helps me understand different ways of accomplishing things!

    Thanks again to you both
    Steve Caponetto - [profile] [e-mail]
    CreedFeed.com - feed your need for Creed!

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It is kind of a funny construct. You would think you would have to put the letters in an array like this:

    $string = array('s', 'i', 't', 'e', ...etc.);
    $lower = array('a', 'b', 'c', ....etc.);

    to be able to refer to them with index values, but php is a pretty smart critter, so if you put a string in a variable, and then use the variable name like an array (e.g. $string[0] or $lower[2]), php will figure out what you mean.

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HumanClay, unfortunately, 7stud's solution is way too overkill and quite inefficient for what you are after, the only reason I say this (no offense 7stud), is because by doing things this way you are well on your way to a poor coding style. There are string functions built in to PHP to cover just about every imaginable situation you may come across. One should always resort to builtin function as opposed to custom algorithyms that normally take the longest possible route to come to the same conclusion. I would suggest you have a look at http://www.php.net/strings
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    uhhhh...poor coding style? lol...There is nothing the matter with the coding style, there is just a standard function that you should use instead. Custom algorithyms that take the longest possible route? 7stud's solution is way too overkill? Hahahhaa. It is only six lines of code. I think you are being a little melodramatic.

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Six lines of code that can be done in one, plus it isn't really six lines of code is it 7stud? You would have everyone here think that looping through 26 elements of an array just to find one match and make it upper case by using a whole different array isn't poor coding? I propose that even if we follow your style wouldn't be better and faster to do this way? That is of course if one really want to try and reinvent the wheel.

    PHP Code:
    <?php
    $string 
    'sitepoint';
    $letters = array("a" => "A","b" => "B","c" => "C","d" => "D","e" => "E","f" => "F",
                     
    "g" => "G","h" => "H","i" => "I","j" => "J","k" => "K","l" => "L",
                     
    "m" => "M","n" => "N","o" => "O","p" => "P","q" => "Q","r" => "R",
                     
    "s" => "S","t" => "T","u" => "U","v" => "V","w" => "W","x" => "X",
                     
    "y" => "Y","z" => "Z",);
    print 
    $letters[$string[0]];
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol...The only thing I would have everyone here think is something they already know.

    Oh, and by the way, if you are going to be using good "style" wouldn't you want to change all those double quotes in your code to single quotes? ....lollllll. I wonder what takes longer, parsing 52 strings or looping through an array on average 13 times?
    Last edited by 7stud; Jun 2, 2001 at 16:10.


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
  •