I have a variable in PHP:
<p><?php echo $this->userInfo->name;?></p>
This outputs their first name and surname (i.e. Joe Bloggs)
I want to only show their first name and the first character of their last name (i.e. Joe B)
I can show the first character of their first name and hide the rest by doing the following in CSS:
p {
visibility: hidden;
}
p::first-letter {
visibility: visible;
}
I’m thinking I could use a function in PHP, along the lines of:
function abbreviateName($this->userInfo->name) {
if($this->userInfo->name == "")
return "";
$tmp = explode(" ", $this->userInfo->name, 2)
if(count($tmp)<=1) {
return ucwords($tmp[0]).".";
} else {
$fn = ucwords($tmp[0]);
$ln = ucwords(substr($tmp[1],0,1);
return $fn.". ".$ln.".";
}
}
But that’s not working
3
Answers
Ok, so I've come up with a nice and simple solution:
Which seems to do the job nicely!
Assuming there’s always exactly one space, you can take the substring of the string from the start to one after the index of the space.
Couldn’t you just write function in PHP to format the name before it is
echo
‘d out?