Show only names in the same variable
<p>I have a variable in PHP: </p>
<pre class="brush:php;toolbar:false;"><p><?php echo $this->userInfo->name;?></p></pre>
<p>This will output their first and last name (i.e. Joe Bloggs)</p>
<p>I only want to display the first character of their first and last name (i.e. Joe B)</p>
<p>I can show the first character of their name and hide the rest by doing the following in CSS: </p>
<pre class="brush:php;toolbar:false;">p {
visibility: hidden;
}
p::first-letter {
visibility: visible;
}</pre>
<p>I thought I could use a function in PHP, something like this: </p>
<pre class="brush:php;toolbar:false;">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.".";
}
}</pre>
<p>But it doesn’t work</p>