View Full Version : how to extract first letter of a word
llizard
12-07-2005, 11:43 PM
Because I'm trying to learn php in a completely backwards fashion (and jumping in the deep end without waterwings), I'm afraid I don't even know where to begin searching for the answer to this. I'm guessing that I have to use preg_split?? or split?? or perhaps explode??
Here's what I'm trying to do:
In a form, a person puts in first and last name. I would like to extract the first letters from each of those names to echo the initials in a different colour (using CSS span, I guess, once the letters were isolated by php). Let's say field one value is "Fred" and field two value is "Jones". I would like to echo the name as:
Fred Jones
I managed to find out how to get the form value to echo the first letter in uppercase... but that doesn't really get me anywhere.
$field1 = ucfirst($field1);
$field2 = ucfirst($field2);
I've now stared at the php.net pages on preg_split, split and explode. Any help would be greatly appreciated.
Element
12-07-2005, 11:54 PM
Because I'm trying to learn php in a completely backwards fashion (and jumping in the deep end without waterwings), I'm afraid I don't even know where to begin searching for the answer to this. I'm guessing that I have to use preg_split?? or split?? or perhaps explode??
Here's what I'm trying to do:
In a form, a person puts in first and last name. I would like to extract the first letters from each of those names to echo the initials in a different colour (using CSS span, I guess, once the letters were isolated by php). Let's say field one value is "Fred" and field two value is "Jones". I would like to echo the name as:
Fred Jones
I managed to find out how to get the form value to echo the first letter in uppercase... but that doesn't really get me anywhere.
$field1 = ucfirst($field1);
$field2 = ucfirst($field2);
I've now stared at the php.net pages on preg_split, split and explode. Any help would be greatly appreciated.
ucwords() upercases words. And if you need to do that you need just the first letter I suppose you could just do.
$first_letter = explode(" ", $field1);
$first_letter = $first_letter[0]{0};
Or something.
Brandoe85
12-08-2005, 12:09 AM
I believe substr() (http://us3.php.net/substr) should help you along, as well.
Good luck
Velox Letum
12-08-2005, 12:17 AM
Or, put the word into a variable...
<?
$word = "Super!";
echo $word{0}; // Outputs 'S' without quotes.
?>
you could also do it with regex
$str = 'Fred Jones Blah blah';
echo preg_replace('/(\w)(\w+)/', '<font color="red">\1</font>\2', $str);
although it may not be the most optimal solution.
llizard
12-08-2005, 11:42 AM
I believe substr() (http://us3.php.net/substr) should help you along, as well.
Yes, indeed, substr() is going to work perfectly. I thought that fci's preg_replace might work too but I do not yet understand what (\w)(\w+) does exactly (ie: what w and/or w+ mean - it's probably something so basic that it doesn't need explaining to people who have started their studies at chapter one instead of much further into the manuals the way I have :rolleyes:).
Many thanks to all for the help!
Element
12-08-2005, 04:29 PM
Yes, indeed, substr() is going to work perfectly. I thought that fci's preg_replace might work too but I do not yet understand what (\w)(\w+) does exactly (ie: what w and/or w+ mean - it's probably something so basic that it doesn't need explaining to people who have started their studies at chapter one instead of much further into the manuals the way I have :rolleyes:).
Many thanks to all for the help!
Here is a function I wrote using substr() for you.
<?php
function FirstLetter($string) {
if(!(empty($string))) {
$string = substr($string, 0, 1);
return $string;
} else {
return "There was an error finding our first letter!";
}
}
echo FirstLetter("Jordan");
?>
marek_mar
12-08-2005, 04:33 PM
$word = 'Jones';
$first_letter_in_word = $word{0};
(\w)(\w+) does exactly (ie: what w and/or w+ mean - it's probably something so basic that it doesn't need explaining to people who have started their studies at chapter one instead of much further into the manuals the way I have :rolleyes:).
Regular expression can be pretty confusing if you're not familiar with it:
http://en.wikipedia.org/wiki/Regex
in a nutshell, a single \w represents the following possible characters a-z, A-Z, 0-9.
in my example the first \w finds a string that that begins with [a-zA-Z0-9]
(\w)(\w+)
the above is \1 in the 2nd argument - the parentheses stores the value it finds.
then anything followed by the single letter/number, it has to match [a-zA-Z0-9]
(\w)(\w+)
the above is \2 in the 2nd argument - the parentheses stores the value it finds.
when it encounters a character that is not [a-zA-Z0-9] it can't match anything, so it has to find something that looks in this pseudo-form:
(Any character that is a-z, A-Z, 0-9)(Any characters that are a-z, A-Z, 0-9)
I would make one change to the pattern though to make it allow for single characters to be highlighted:/(\w)(\w+)?/
the ? makes the preceding parenthetical item optional (so that the single character \w doesn't need to be followed by any of the [a-zA-Z0-9] chracters).
Element
12-08-2005, 04:53 PM
Okay... this is way dragged out for a simple question. So... here, this does exactly what you want, with even a full string. Just replace the <*font></font> tag with whatever your CSS tag for it is.
<?php
function NameMarkup($string) {
if(!(empty($string))) {
if(strpos($string, " ")) {
$string = explode(" ", $string);
$count = count($string);
$new_string = '';
for($i = 0; $i < $count; $i++) {
$first_letter = substr(ucwords($string[$i]), 0, 1);
$new_string .= "<font color=\"red\">".$first_letter."</font>".substr($string[$i], 1, strlen($string[$i])-1)." ";
}
return $new_string;
} else {
$first_letter = substr(ucwords($string), 0, 1);
$string = "<font color=\"red\">".$first_letter."</font>".substr($string, 1, strlen($string)-1)." ";
return $string;
}
} else {
return "The string was empty!";
}
}
echo NameMarkup("jordan thompson");
?>
That capitilizes the names and then outputs. Example: http://amerikanmetz.freeownhost.com/firstltetter.php
Velox Letum
12-08-2005, 05:49 PM
Why do you even bother using substr for such a basic use? As I said in my post above:
<?
$word = "Super!";
echo $word{0}; // Outputs 'S' without quotes.
?>
Using curly braces and a number returns the character of the specified position. substr() is an over-complication in this circumstance.
marek_mar
12-08-2005, 05:55 PM
I feel ignored :rolleyes: .
Element
12-08-2005, 05:59 PM
Not exactly, considering its already set and ready to handle names and do all the work. It is what would conventionally be done in this instance instead of formating each name part using curly brackets, which would return the first letter, meaning if he wanted to display the rest the whole name would still be there. He is asking how to extract the letter to do a color markup on it using CSS... so curly brackets seems like the more complicated way.
Using this function is quite simple in a script. Place the function in the functions file (or where ever) and go for it. Like...
// Some users profile
echo "<p align=\"center\">".NameMarkup($first_name . " " . $last_name)."</p>";
So then its easier then getting the first letter, styling it, and then figuring out how to get the rest of the name to output it without the style. and then doing it for the next name (if any).
marek_mar
12-08-2005, 06:16 PM
One line:
<?php
$name = 'john';
print substr_replace($name, '<span style="color: red;">' . strtoupper($name{0}) . '</span>', 0, 1);
?>
Element
12-08-2005, 06:23 PM
One line:
<?php
$name = 'john';
print substr_replace($name, '<span style="color: red;">' . strtoupper($name{0}) . '</span>', 0, 1);
?>
Lol, you guys are obsessed with compact code that is functionality wise limited.
That turns out too be more then one line of codde, and more code in a place where you want less. With that you have to repeate it for each word.
See your trying to make it smaller when your making it bigger, what is the ideal way to compact your code? Using functions. That is the only way.
Both your ways put more code into the script where as when you are trying to make it smaller your making it larger.
Functions aren't considered part of the script, more as a library the script uses. There for once again using a simple NameMarkup() function in the code makes it as small as it will get. As well as it handles multiple names, or a whole string if someone finds the need for it that way.
marek_mar
12-08-2005, 07:08 PM
Using functions.
And YOU were against classes?
You can make that into a function if you need to. This is the anwser for the problem. There was nothing about implementing it or replacing all first letters in a string. If I'd have to replace all first letters with <span...</span> I'd probably use regex.
<?php
$name = "john doe and his children";
echo preg_replace_callback("/([a-z])([^\s]+)/i", create_function('$matches', 'return \'<span style="color: red;">\' . strtoupper($matches[1]) . \'</span>\' . $matches[2];'), $name);
?>
But as long as he has two fields with one word in it I don't think a regex is needed.
BTW This line has a function declaration so with your way of thinking it shouldn't be counted as "part of the code".
Velox Letum
12-08-2005, 07:21 PM
I agree with marek...why use a long function for something that can be done in a single line of code? If the line of code is too long, then you can easily make a function with it. If he only has a first name and last name you don't even need regex as marek said.
llizard
12-09-2005, 05:07 AM
Wow!! I had no idea that this would cause such a discussion. Thanks fci for explaining about \w and \w+
I have used the following because it works and I understand it.
$firstname = 'Fred';
$lastname = 'Jones';
echo '<span style="color:red">'.$firstname{0}.'</span>'.substr($firstname1, 1).' <span style="color:blue">'.$lastname{0}.'</span>'.substr($lastname1, 1);
above outputs:
Fred Jones
which is exactly what I wanted to achieve.
I'll stare at the other coding options that were set out by you all and in time might come to understand what has been done there too.
Many thanks for the help!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.