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
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
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):
Bookmarks