SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot DewChugr's Avatar
    Join Date
    Sep 2005
    Location
    Illinois
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help getting first letter of words in string

    I need to take the first letter of the first (up to) four words of a string that contains 1 or more words and make a new lowercase string out of those letters. Example, "The quick brown fox ate the slow chicken" would be "tqbf". What would be the best way to do this? Some of the letters may be special such as .

    Thanks for the help.

    Steve

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    strtolower, explode, for loop and $word{0} would do that.

  3. #3
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function get_first_letters($string)
    {
      return 
    preg_replace('/(\B.|\s+)/','',$string);
    }

    $str 'The quick brown fox ate the slow chicken';

    $first_letters strtolower(substr(get_first_letters($str),0,4));

    var_dump($first_letters); 
    Note that you might have to use mb_substr and mb_strtolower (if available) depending on the character set.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Location
    Toronto
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I am a beginner to php as well, here is my attempt at the code, if there are problems, please point it out, thx.

    PHP Code:
    <?php

    $string 
    $_POST['string'];
    $explodedString explode(" ",$string);
    for(
    $i 0$i <4$i++){
        
    $tempString substr($explodedString[$i], 01);
        
    $finalString .= $tempString;
    }
    $finalString strtolower($finalString);
    echo 
    $finalString;

    ?>
    In search of the clouds...and maybe some work

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $tempString = $explodedString[$i]{0}

  6. #6
    SitePoint Addict
    Join Date
    Mar 2005
    Location
    Toronto
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by iomanip
    $tempString = $explodedString[$i]{0}
    is this the faster way to obtain the first letter of a string?
    In search of the clouds...and maybe some work

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think so, because a string is normally an array of chars. So you are accessing the char directly without using a function that cuts that string.

  8. #8
    SitePoint Addict
    Join Date
    Mar 2005
    Location
    Toronto
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, but I don't really understand the {0} part though, is that 0 a pointer to the string? Care to explain a bit please, and thanks
    In search of the clouds...and maybe some work

  9. #9
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kefeso
    PHP Code:
    <?php

    $string 
    $_POST['string'];
    $explodedString explode(" ",$string);
    for(
    $i 0$i <4$i++){
        
    $tempString substr($explodedString[$i], 01);
        
    $finalString .= $tempString;
    }
    $finalString strtolower($finalString);
    echo 
    $finalString;

    ?>
    1. If $_POST['string'] does not exist, E_NOTICE
    2. preg_split('/\s+/',$string) would be better because explode(' ',$string) will not split at newlines
    3. You do not initialise $finalString, E_NOTICE
    4. ++$i is faster than $i++ (++$i increments $i and returns the new value, $i++ returns $i's current value and then increments it, but here the return value is of no interest to us so ++$i is better because it is faster)
    5. $tempString is useless, you could append directly to $finalString
    6. If there are less than 4 words in $_POST['string'], you'll get a bunch of undefined index E_NOTICEs because the loop just goes on anyway

    substr is just fine, {} is actually going to get deprecated in PHP6 in favor of [] (it used to be the other way around)

  10. #10
    SitePoint Addict
    Join Date
    Mar 2005
    Location
    Toronto
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for taking the time to look at my code and writing the comments. Appreciate it!
    In search of the clouds...and maybe some work

  11. #11
    SitePoint Zealot DewChugr's Avatar
    Join Date
    Sep 2005
    Location
    Illinois
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sorccu
    PHP Code:
    function get_first_letters($string)
    {
      return 
    preg_replace('/(\B.|\s+)/','',$string);
    }

    $str 'The quick brown fox ate the slow chicken';

    $first_letters strtolower(substr(get_first_letters($str),0,4));

    var_dump($first_letters); 
    Note that you might have to use mb_substr and mb_strtolower (if available) depending on the character set.
    Thanks for the help, I like this code. Can you help me with this regular expression? I get the \s, but I don't get how the \B. works.

    Also, about the ++$i vs. $i++, it sounds like in most cases it is better to use ++$i in loops?

    Thanks,
    Steve

  12. #12
    SitePoint Enthusiast Salkcin's Avatar
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres a way with regular_expressions()

    PHP Code:
    <?php
    $string 
    "The quick brown fox ate the slow chicken";
    $chars substr(preg_replace('/\b(\w+)/eis''strtolower(substr(\\1, 0, 1))'$string4), 08);

    echo 
    str_replace(' '''$chars); // output: tqbf
    ?>

  13. #13
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DewChugr
    I get the \s, but I don't get how the \B. works.
    \B means "not a word boundary", and it is an assertion so although its presence is checked it is not actually included in the resulting match

    Quote Originally Posted by PHP Manual
    A word boundary is a position in the subject string where the current character and the previous character do not both match \w or \W (i.e. one matches \w and the other matches \W), or the start or end of the string if the first or last character matches \w, respectively.
    Quote Originally Posted by DewChugr
    Also, about the ++$i vs. $i++, it sounds like in most cases it is better to use ++$i in loops?
    In for-loops as the third argument, yes, because the return value does not matter. However if the return value matters you have to decide which one you want to use. For example

    Code:
    $i = 0;
    
    while (++$i <= 3)
    {
      echo $i . "\n";
    }
    outputs 1 2 3, whereas

    Code:
    $i = 0;
    
    while ($i++ <= 3)
    {
      echo $i . "\n";
    }
    outputs 1 2 3 4

  14. #14
    SitePoint Zealot DewChugr's Avatar
    Join Date
    Sep 2005
    Location
    Illinois
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again for the help!


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
  •