I have a variable in PHP:
userInfo->name;?>
This will output their first and last name (i.e. Joe Bloggs)
I only want to display the first character of their first and last name (i.e. Joe B)
I can show the first character of their name and hide the rest by doing the following in CSS:
p { visibility: hidden; } p::first-letter { visibility: visible; }
I thought I could use a function in PHP, something like this:
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 it doesn’t work
Assuming there is always a space, you can index from the beginning of the string to a substring after the space.
Okay, so I came up with a nice and simple solution:
Looks like it works great!