Page 1 of 1
[SOLVED]Stripping a string down to the first chars of each w
Posted: Tue Oct 31, 2006 12:42 pm
by batfastad
Hi guys
I'd like to know the best way to extract the first characters of each word in a string.
This would have 2 uses - converting people's names to initials, and also converting a string into an abbreviation.
The way I thought of doing it is...
Code: Select all
$string = 'John McGee';
$string_exp = explode(' ', $string);
foreach ($string_exp as $key => $word) {
$abbr .= substr($word, 0, 1);
}
echo strtoupper($abbr);
But I'm not sure if this is the best way of doing things.
Is there a quicker / more efficient way I'm missing here?
Thanks
Ben
Posted: Tue Oct 31, 2006 1:35 pm
by volka
Code: Select all
$pattern = '!(?<=\s|^)(\S)!';
$subject = 'Mary had a little lamb, its fleece was white as snow';
preg_match_all($pattern, $subject, $matches);
foreach($matches[1] as $m) {
echo "'", $m, "' <br />\n";
}
I don't know if it's more efficient but it's different

Posted: Tue Oct 31, 2006 1:46 pm
by Superman859
I thought of doing it the same way you did, although I'm not sure if it is the best (I just started learning PHP last week).
One thing to note...if you are not dealing with keys, you don't need to write $key.
Code: Select all
$string = 'John McGee';
$string_exp = explode(' ', $string);
foreach ($string_exp as $word) {
$abbr .= substr($word, 0, 1);
}
echo strtoupper($abbr);
However, from what I understand, if you continue with another string (for example if you made a loop and were going to get the initials of many users), you would want to reset $abbr each time. Otherwise it would continue adding on to the first initials/etc. I could be wrong, I'm just beginning, but I do come from having a strong Java background.
I also believe you could do the following...
Code: Select all
$string = 'John McGee';
$string_exp = explode(' ', $string);
foreach ($string_exp as $word) {
$abbr .= $word{0}; //I believe this is similar to charAt() in other languages...returns the character at given position.
}
echo strtoupper($abbr);
Posted: Tue Oct 31, 2006 2:59 pm
by Weirdan
here's another idea:
Code: Select all
$abbr = join(
'',
array_filter(
preg_split(
'//',
ucwords(
strtolower($string)
)
),
'ctype_upper'
)
);
Posted: Tue Oct 31, 2006 5:50 pm
by batfastad
Superman859 wrote:One thing to note...if you are not dealing with keys, you don't need to write $key.
Yep, well spotted!
Was just testing
Superman859 wrote:However, from what I understand, if you continue with another string (for example if you made a loop and were going to get the initials of many users), you would want to reset $abbr each time. Otherwise it would continue adding on to the first initials/etc. I could be wrong, I'm just beginning, but I do come from having a strong Java background.
Again, yep. I will do that, as I'll set this up as a function.
Superman859 wrote:$abbr .= $word{0}; //I believe this is similar to charAt() in other languages...returns the character at given position.
Out of all these different suggestions, explode() seems the simplest one.
I try to steer clear of regular expressions unless they're absolutely needed as I always thought there's a processing overhead, but also because I don't understand regex syntax
Anyone have any info on this type of syntax?
Where would I find it in the PHP manual?
I'm assuming that since this syntax seems to be a language construct, it should give better performance than using a function like substr()?
Not that it really matters for such a small function. Just like to know I'm doing things the best way!!
Thanks for all your help everyone!!!
Ben
Posted: Tue Oct 31, 2006 10:16 pm
by printf
One more way...
Code: Select all
<?php
$string = 'John McGee';
echo implode ( ' ', array_map ( create_function ( '$v', 'return $v[0] . ".";' ), explode ( ' ', $string ) ) );
?>
printf
Posted: Wed Nov 01, 2006 3:18 am
by Jenk
and another..
Code: Select all
$string = 'The Quick Brown Fox Jumped Over The Lazy Dog.';
echo preg_replace('/[a-z.,!\?]+\s?/m', '', $string);
//output: TQBFJOTLD
Posted: Wed Nov 01, 2006 3:23 am
by volka
batfastad wrote:Anyone have any info on this type of syntax?
Where would I find it in the PHP manual?
It's in the description of the string type,
http://www.php.net/manual/en/language.t ... ote]String access and modification by character
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array-brackets like $str[42] so think of a string as an array of characters.
Note: They may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred because the {braces} style is deprecated as of PHP 6. [/quote]
Posted: Wed Nov 01, 2006 4:38 am
by batfastad
Awesome, thanks for all the help guys!