Search code examples
phparraysvbulletin

PHP Warning: Illegal string offset 'username' in ..../memberlist.php(876) : eval()'d code on line 6


I'm trying to get the usernames of the members on my vbulletin forum so I can use the first letter of their name as an avatar. I tried the normal way and a few others but I've not been successful. I think it is due to the info being in an array. This is my code for the first letter.

//get the username
$username = $userinfo['username']; 
//strip to get the first letter
$letterUsername['username'] = substr($username, 0, 1); 
//capitalize the first letter 
$letterUsername['username'] = strtoupper($letterUsername['username']); 
//set it as a variable 
$letterUsername = $letterUsername['username'];  

I get the error in the title when I run this code on the memberlist.php page. Line 876 is the hook:

($hook = vBulletinHook::fetch_hook('memberlist_bit')) ? eval($hook) : false;

How can I get the username for all members on the page using the above code? I don't have much experience with arrays and really need help. Here is the memberlist.php file: https://pastebin.com/wfgLikJ2


Solution

  • Another way to get that 1st letter and capitalize it:

    // I find it easier just to work with arrays, less functions to memorize
    // [0] to get 0 index because str_split() converts string to an array
    // Remember, functions are evaluated from innermost to outermost  
    $letterUserName = strtoupper(str_split($userinfo['username'])[0]);
    
    // or NOT converting to an Array, both work
    // [0] here because string characters can be accessed with []
    // "Hello"[1] === "e" 
    $letterUserName2 = strtoupper($userinfo['username'][0]);
    

    I try to keep as much stuff on 1 line as possible otherwise your code will go from 200 lines to 400 lines super fast, making it overwhelming.

    I tested it and it works ^_^ enter image description here