Jump to content

HOW TO REDUCE NAME TO INITIAL...


Go to solution Solved by cgus,

Recommended Posts

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

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
  • Solution
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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.