SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Print initials of a name

    $name ="Bill Gates Company";

    I need a script that would look at the $name variable and extract initials of the first two names, so that I get an output like "BG".

    Does anybody have a solution handy or give any possible directions?

    Thanks a lot!
    andROO

  2. #2
    SitePoint Addict y_oda2002's Avatar
    Join Date
    May 2005
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know if this would be the most efficient way to do it but you could use a loop the goes until the end of the string...
    Keep the first character you hit and the next character after the first space you hit.
    PHP Code:


    $quit 
    false;

    //Go till end of length or until you found 2nd character
    while($ctr != $length && !$quit)
    {

      
    //If the character is a space then the next character is probably the one you want
      
    if($name[ctr] == ' ')
      {
        
    $secondCharacter $name[ctr+1];

        
    //If it was the character you wanted then set flag to true to quit loop
        
    if($secondCharacter != ' ')
           
    $quit true;
      }
    }

    //Get first character
    $firstCharacter $name[0];

    //Concatenate the characters
    $initial $firstCharacter $secondCharacter
    Its not the best but I hope it gives you an idea...

    Nick

  3. #3
    SitePoint Enthusiast badg0003's Avatar
    Join Date
    Aug 2004
    Location
    Ottawa, Ontario
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use the function explode to split the string into an array.
    (http://ca.php.net/manual/en/function.explode.php).

    i.e.
    Code:
    $name = "Bill Gates Company";
    
    $arrName = explode(" ", $name);
    
    /* contents of $arrName
    
    $arrName[0] = "Bill"
    $arrName[1] = "Gates"
    $arrName[2] = "Company";
    
    */
    You mentioned you wanted just the first and last name, so in this case the only parts of the array you'll be interested in are $arrName[0] (first name) and $arrName[1] (last name). To get the initial of each, use:

    Code:
    $arrName[0][0]; // First name, first letter of name
    $arrName[1][0]; // Last name, first letter of name

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    In home!
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, but... thats kinda hard, because how will PHP know that the word Company is not an initial of the name?

    Because you can do it automatically using some string functions, but... that will returns:

    BGC

    All of your vars have three words? and you need only the initials of the first two words?
    Takitei
    FAQtion.com - Get answers to your development FAQs now!

  5. #5
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another way:
    PHP Code:
    preg_match_all('~\b(\S){1}~'$name$matches);
    echo 
    $matches[1][0].$matches[1][1]; 

  6. #6
    SitePoint Addict y_oda2002's Avatar
    Join Date
    May 2005
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All of your vars have three words? and you need only the initials of the first two words?
    Thats what badg003 used this part is for...

    Code:
    $arrName[0][0]; // First name, first letter of name
    $arrName[1][0]; // Last name, first letter of name
    your only getting the first character of the first two words...
    assumption = the first two words are the first and last name...
    Nicholas Rhodes
    Asgard Development
    http://www.asgard-dev.com

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    In home!
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    Another way:
    PHP Code:
    preg_match_all('~\b(\S){1}~'$name$matches);
    echo 
    $matches[1][0].$matches[1][1]; 
    I would choose that one. It looks faster for PHP. Let me know if I'm wrong.
    Takitei
    FAQtion.com - Get answers to your development FAQs now!

  8. #8
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    Another way:
    PHP Code:
    preg_match_all('~\b(\S){1}~'$name$matches);
    echo 
    $matches[1][0].$matches[1][1]; 
    Hi Mark,

    This is performing just what I need! Even though some names are composed of 3 and more parts, your code only outputs the initials of the first two which is just perfect! I can see how it does the trick, and can add more characters if needed. The idea was to use initials to generate 2-letter prefixes as part of a reference, sort of BG-327452, where the name isn't necessarily a real name, so I'm not bothered.

    Thank you all for your inputs! I made notes of the scripts you offered for future checks, thanks

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a new goal now.
    PHP Code:
    $name ="Bill Gates";
    preg_match_all('~\b(\S){1}~'$name$i);

    echo 
    $i[1][0].$i[1][1]; 
    - this will output BG

    How do I make it BGa - that is a combination of the first 1 letter from word 1 and first 2 letters from word 2. Is it difficult?

  10. #10
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's stretching the limits of my regex knowledge, but try:
    PHP Code:
    preg_match('~(\S)\w*?\s(\S{2})~'$name$i);
    echo 
    $i[1].$i[2]; 

  11. #11
    SitePoint Wizard
    Join Date
    Jan 2004
    Location
    3rd rock from the sun
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a thought.. would it work better with Linus Torvalds?

  12. #12
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably, using Bill Gates as the test name makes the expression greedy.

  13. #13
    SitePoint Wizard
    Join Date
    Jan 2004
    Location
    3rd rock from the sun
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Sauce, open sauce..

  14. #14
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agree(dy) mark, however I also believe that Bill Gates doesn't mind a second. I wouldn't, if I were the account(s) holder in his place Going to try your option in a second, hang on...

  15. #15
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    It has actually worked,
    thanks a lot, Mark and the others!

  16. #16
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    In home!
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    It's stretching the limits of my regex knowledge, but try:
    PHP Code:
    preg_match('~(\S)\w*?\s(\S{2})~'$name$i);
    echo 
    $i[1].$i[2]; 
    Very good regex Mark... very good.
    Takitei
    FAQtion.com - Get answers to your development FAQs now!


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
  •