Page 1 of 1
Chopping characters off of strings?
Posted: Thu Aug 22, 2002 7:06 pm
by Matt Phelps
I'm looking for a way to turn long names (eg; Brandon Bergenroth) into much shorter names by removing the first name and converting it into just an initial - so my example would now be 'B Bergenroth'.
I've managed to chop the name up into two variables using the explode function but I'm not sure how I turn 'Brandon' into just 'B'.
Here's what I have so far, does anyone have the second step?
Code: Select all
$name = "Brandon Bergenroth";
$name_length = strlen($name);
if ($name_length>12)
{
$newname = explode(" ",$name);
$first_name = "$newnameї0]";
}
Posted: Thu Aug 22, 2002 7:11 pm
by DSM
use
Code: Select all
$first_int = substr($first_name,0,1);
Posted: Thu Aug 22, 2002 7:36 pm
by Matt Phelps
Thanks
In case anyone is looking for a similar solution:
Code: Select all
function chopname($name)
{
$name_length = strlen($name);
if ($name_length>17)
{
$long_name = explode(" ",$name);
$first_name = "$long_nameї0]";
$surname = "$long_nameї1]";
$initial = substr($first_name,0,1);
$short_name = array ($initial, $surname);
$name = implode(" ", $short_name);
}
return $name;
}
Posted: Thu Aug 22, 2002 10:00 pm
by DSM
NP...
had the same thing come up today, creating directories with user first intial and lastname as the name of the dir
Posted: Fri Aug 23, 2002 4:24 am
by mikeq
DSM wrote:NP...
had the same thing come up today, creating directories with user first intial and lastname as the name of the dir
What happens if 2 or more people have the same name?
Posted: Fri Aug 23, 2002 5:27 am
by 9902468
mikeq wrote:DSM wrote:NP...
had the same thing come up today, creating directories with user first intial and lastname as the name of the dir
What happens if 2 or more people have the same name?
Before you create you check if there is already dir with that name, if there is, put a number after the lastname. (eg. ajhonson and ajhonson1 etc...) Remember to check also if there are already same name with a number.... or you could also use in a case like this a few special characters that are allowed in directory names like ajhonson and a_jhonson.
(obvious, no?)
-9902468