cgus Posted April 29, 2015 Share Posted April 29, 2015 Trying to output only the first character of a name in a field -- like, "Andrew" would output as "A." I have this: <?php echo $row_rs1[left('fname', 1)]; ?> It does not work, but causes this error: Fatal error: Call to undefined function LEFT() in.... Any guidance will be appreciated! Link to comment https://forums.phpfreaks.com/topic/295946-how-to-reduce-name-to-initial/ Share on other sites More sharing options...
Muddy_Funster Posted April 29, 2015 Share Posted April 29, 2015 substr('fname', 0, 1); Link to comment https://forums.phpfreaks.com/topic/295946-how-to-reduce-name-to-initial/#findComment-1510291 Share on other sites More sharing options...
cyberRobot Posted April 29, 2015 Share Posted April 29, 2015 Just to clarify, is the first name stored in $row_rs1['fname']? If so, you can modify Muddy_Funster's suggestion to: <?php echo substr($row_rs1['fname'], 0, 1); ?> Link to comment https://forums.phpfreaks.com/topic/295946-how-to-reduce-name-to-initial/#findComment-1510295 Share on other sites More sharing options...
Zane Posted April 29, 2015 Share Posted April 29, 2015 If you only need the first character of a string there is no reason to use substr, when you can easily access that first character directly, as if it were an array. $myString = "abcdefg"; echo $myString[0]; //will echo ==> a Link to comment https://forums.phpfreaks.com/topic/295946-how-to-reduce-name-to-initial/#findComment-1510297 Share on other sites More sharing options...
cgus Posted April 29, 2015 Author Share Posted April 29, 2015 Thanks for your responses. I tested the suggestions by Guru and Administrator. Both work perfectly. PHP to get only the first character in a string to output. Example: "Andrew" outputs to "A" <?php echo substr($row_rs1['fname'], 0, 1); ?> Tested this and it works. $myString = "abcdefg"; echo $myString[0]; //will echo ==> a Tested this, and it also works. Implemented as: <?php echo $row_rs1['fname'][0]; ?> Grateful! Link to comment https://forums.phpfreaks.com/topic/295946-how-to-reduce-name-to-initial/#findComment-1510304 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.