Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Joe Bruno
35,909 PointsPHP Need help converting to a string
<?php
function create_initials() {
get_currentuserinfo();
$string = "First Last" . // need this to be dynamic: $current_user->user_firstname . $current_user->user_lastname) ;
$expr = '/(?<=\s|^)[a-z]/i';
preg_match_all($expr, $string, $matches);
$result = implode('', $matches[0]);
$result = strtoupper($result);
echo $result; }?>
Thank You!
6 Answers
Chris Southam
1,823 PointsIf I understand rightly you are looking for a function to return the initials of the name you input. You've over complicated it a little!
function create_initials($firstname, $lastname) {
return strtoupper(substr($firstname, 0, 1) . substr($lastname, 0, 1));
}
And then use it like this: create_initials($current_user->user_firstname, $current_user->user_lastname);
Joe Bruno
35,909 PointsWhen I run:
<?php echo create_initials($current_user->user_firstname, $current_user->user_lastname); ?>
The function echoes out: "$$"
It is not treating the variables as variables. It is treating them as strings and running the function accordingly - taking the first letter of each variable which is the $. What am I doing wrong still?
Chris Southam
1,823 PointsSo what is $current_user? Where has it come from? Is it an object or array?
Joe Bruno
35,909 PointsIt is an object I am pulling from a wordpress plugin into another part of my project. Here is the full code for my content-user.php:
<?php
global $wpdb, $pmpro_msg, $pmpro_msgt, $pmpro_levels, $current_user, $levels, $pmpro_currency_symbol;
//if a member is logged in, show them some info here (1. past invoices. 2. billing information with button to update.)
if($current_user->membership_level->ID)
{
?>
<?php
function create_initials($firstname, $lastname) {
get_currentuserinfo();
return strtoupper(substr('$firstname', 0, 1) . substr('$lastname', 0, 1));
}
?>
<div id="pmpro_account-profile" class="pmpro_box row">
<div class="circle"><?php echo create_initials($current_user->user_firstname, $current_user->user_lastname); ?></div>
<h3 class="user-icon col-xs-12"><?php echo $current_user->user_login?></h3>
<p class="col-xs-12"> Hello <?php echo $current_user->user_lastname?> <?php echo $current_user->user_lastname?>, <br> Welcome back....</p>
<h5 class="col-xs-12"><?php echo $current_user->user_email?></h5>
<div class="col-xs-12 edit-profile">
<a href="<?php echo admin_url('profile.php')?>"><?php _e("Edit Profile", "pmpro");?></a>
</div>
</div> <!-- end pmpro_account-profile -->
<?php } ?>
Chris Southam
1,823 PointsTake out the single quotes in the function and you shouldn't need get_currentuserinfo() in there.
return strtoupper(substr($firstname, 0, 1) . substr($lastname, 0, 1));
Joe Bruno
35,909 PointsThat did it. It is working now. Thank you.