Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2003
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get first letter from string..

    How do I get the first letter of a string? I can't seem to figure it out...

    for example: '23V6', I want to get the 'V'

    Thanks for reading

  • #2
    Regular Coder
    Join Date
    Jan 2003
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was having the same problem yesterday. Heres how:

    First, define the function you will use:
    PHP Code:
    function str_split($string) {
       for (
    $x 0$x strlen($string); $x++) {
           
    $array[$x] = substr($string$x1);
       }
       return 
    $array;

    Then, do the code to get the string:
    PHP Code:
           $text "23V6";
           
    $string str_split($text1);
           
    $string $string[3];

           echo 
    $string
    That should do it. The number in the brackets is the number of the letter you want from the string...

  • #3
    New Coder
    Join Date
    Apr 2003
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fast response Thanks!

    And now why didn't I think of that..

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't get it.

    First off, i don't understand that function at all. It's completely unnescecary, because
    PHP Code:
    $text='23V45';
    $string=$text{3}; 
    would give me exacly the same, without needing the function to first turn the string into an array of 1 character elements.

    Then i don't understand how all this gets you the first letter.
    I mean you here hardcode the position.
    In fact, it is wrong, because $string[3] will return '6' because it's a zero based array.

    I thought you wanted to dynamically get the first letter from any string, or is it always the third character?

    Anyway, the dynamic version would be something like:
    PHP Code:
    $text "23V6"
    for (
    $i 0$i strlen($text); $i++) { 
        if (
    eregi("[a-z]"$text{$i})) {
           
    $string $text{$i};
           break(); 
       }

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    New Coder
    Join Date
    Apr 2003
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I did want to do it dynamically.. but his answer gave me the insight to make the loop myself... I didn't want to bother you lot with it anymore since I could figure it out with the part I got from levi.. but I will ofcourse update it with the better solution you posted raf.. it's way more elegant than mine hehe

  • #6
    Regular Coder
    Join Date
    Jan 2003
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops, forgot to post that that was for older versions of php that dont support the str_split command

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You don't necessarily need to define a new function for this problem:

    PHP Code:
    preg_match('/^[^a-z]*([a-z])/im'$string$matches); 
    Using the code above, the first letter in a string will appear in $matches[1].
    De gustibus non est disputandum.

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    and for no other reason whatsoever other than to be contrary ...

    PHP Code:
    <?
    echo substr$text,( strspn$text "1234567890" ) +) ,) ; 
    ?>
    ok ok I admit it , it's cos I can't do regex
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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