I need urgent help - get first character from word.

Discussion in 'PHP' started by G3n3s!s, Feb 4, 2011.

  1. #1
    Hello. I am creating crawler but I am stucked at stupid thing.

    <?php
    $string = "hello";
    
    PHP:
    how could I get firs character of string, in this case "h" ?

    EDIT: I got it,
    sorry
     
    G3n3s!s, Feb 4, 2011 IP
  2. senth

    senth Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you have the answer
    substr($string,0,1);
     
    senth, Feb 4, 2011 IP
  3. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #3
    so why are you replying ? :D
    Thx anyway :p
     
    G3n3s!s, Feb 4, 2011 IP
  4. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #4
    just a tip:
    you can also do this
    
    $string = "hello";
    echo $string[0];
    
    Code (markup):
    and you might need to check if it's not empty:
    
    if(!empty($string)) {...}
    //or
    if(isset($string[0])) {...}
    
    Code (markup):
     
    gapz101, Feb 4, 2011 IP
  5. jhine

    jhine Active Member

    Messages:
    25
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    53
    #5
    I'm just posting to inform you of bad practice. The second and third parameters are int, not string so doing something like substr($string, "0", "1"); is wrong.
     
    jhine, Feb 5, 2011 IP
  6. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #6
    okay, thanks for warn
     
    G3n3s!s, Feb 5, 2011 IP