www.webdeveloper.com
Results 1 to 6 of 6

Thread: Find the first space in a string and return the first letter after it

  1. #1
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    264

    Exclamation Find the first space in a string and return the first letter after it

    Ok, so I have values like this:
    PHP Code:
    // Names:

    $first 'John Smith';
    $second 'Jane Doe, MD'
    etc... I need to display "John S" or "Jane D" and I cannot, for the life of me, remember how to do it... I know it's fairly simple, I just can't remember the right combination of string functions to get the parts that I need.

    Any suggestions?

    EDIT:

    For just the first name, I have this:

    PHP Code:
    substr($row['name'], 0,strpos($row['name'], ' ')) 
    Last edited by amandaNHT; 09-16-2011 at 01:23 PM. Reason: Adding more Information

  2. #2
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    264

    Talking

    Ok, nevermind, I got it!

    In case you were wondering, this works:

    PHP Code:
    substr($row['name'], 0,(strpos($row['name'], ' ')+2)) 

  3. #3
    Join Date
    May 2010
    Posts
    77
    I see you got it.

    I was going to suggest using something like
    Code:
    $name = explode(" ", $first);
    and then use substr to get the first letter out of $name[1]

    but the way you got it looks better.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,157
    The strpos() thing is probably best, but just for the fun of it:
    PHP Code:
    <?php
    $text 
    'John Smith and so forth';
    preg_match('#^[^ ]* +[^ ]#'$text$matches);
    echo 
    $matches[0]; // 'John S'
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    Use an anonymous func

    PHP Code:

    <?php

    $n 
    = function($str){return $x strpos(" ",$str)) ? @$str[$x+1] : false;};
    $string "foo";
    echo 
    $n($string);

    ?>
    Didn't test it, I always have syntax errors on anonymous functions tho, so you might have to add a quote or ;{}()

    Edit: But for the sake of learning something, a "STRING" is really just an array of characters, so it's completely valid to do the following (unless your character is at the end of the string):

    PHP Code:
    <?php
    $string 
    "foobarbaz";
    echo 
    $string[strpos($string"b")+1];
    ?>
    can't do python slices...... yet[::2]
    Last edited by eval(BadCode); 09-19-2011 at 08:39 AM.
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  6. #6
    Join Date
    Jul 2012
    Posts
    29
    Quote Originally Posted by NogDog View Post
    The strpos() thing is probably best, but just for the fun of it:
    PHP Code:
    <?php
    $text 
    'John Smith and so forth';
    preg_match('#^[^ ]* +[^ ]#'$text$matches);
    echo 
    $matches[0]; // 'John S'
    ?>
    Hmm..
    It goes crazy as long as I don't understand the logic behind it !!

    This doesn't find the first char (on position 0):
    PHP Code:
    if (strpos($name$pos))
            echo 
    strpos($name$pos);
        else
            echo 
    "Not found!"
    Neither does this:
    PHP Code:
    if (strpos($name$pos) === true)
            echo 
    strpos($name$pos);
        else
            echo 
    "Not found!"
    But this is OK

    PHP Code:
    if (strpos($name$pos) !== FALSE)
            echo 
    strpos($name$pos);
        else
            echo 
    "Not found!";

    //or


    if (strpos($name$pos) === FALSE)
            echo 
    "Not found!");
        else
            echo 
    strpos($name$pos); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles