How do i get the first character of a string in php?

I need to get the first character of the given string. here i have a name in the session variable. i am passing the variable value to the substr to get the first character of the string. but i couldn't.

i want to get the first character of that string.

for example John doe. i want to get the first character of the string j. how can i get it using php?

  

asked Nov 24, 2015 at 10:11

4

For the single-byte encoded (binary) strings:

substr($username, 0, 1);
// or 
$username[0] ?? null;

For the multi-byte encoded strings, such as UTF-8:

mb_substr($username, 0, 1);

How do i get the first character of a string in php?

answered Nov 24, 2015 at 10:14

How do i get the first character of a string in php?

1

Three Solutions Sorted by Robustness

1. mb_substr

It takes into consideration text encoding. Example:

$first_character = mb_substr($str, 0, 1)

2. substr

Example:

$first_character = substr($str, 0, 1);

3. Using brackets ([])

Avoid as it throws a notice in PHP 7.x and a warning in PHP 8.x if the string is empty. Example:

$first_character = $str[0];

answered Sep 30, 2021 at 3:34

Nabil KadimiNabil Kadimi

9,5792 gold badges49 silver badges57 bronze badges

1

Strings can be seen as Char Arrays, and the way to access a position of an array is to use the [] operator, so there's no problem at all in using $str[0] (and I'm pretty sure is much faster than the substr method).

$fstchar = $username[0];

faster than all method

answered Nov 24, 2015 at 10:19

How do i get the first character of a string in php?

Parth ChavdaParth Chavda

1,7911 gold badge20 silver badges29 bronze badges

Strings in PHP are array like, so simply

$username[0]

How do i get the first character of a string in php?

mega6382

8,96116 gold badges47 silver badges68 bronze badges

answered Nov 24, 2015 at 10:14

How do i get the first character of a string in php?

Szymon DSzymon D

3231 silver badge13 bronze badges

I do not have too much experience in php, but this may help you: substr

Nabil Kadimi

9,5792 gold badges49 silver badges57 bronze badges

answered Nov 24, 2015 at 11:47

Vitor FerreiraVitor Ferreira

1531 gold badge3 silver badges11 bronze badges

If the problem is with the Cyrillic alphabet, then you need to use it like this:

mb_substr($username, 0, 1, "UTF-8");

answered Nov 24, 2021 at 7:49

How do i get the first character of a string in php?

alexsoinalexsoin

711 silver badge5 bronze badges

How do you extract the first character of a string?

To get the first and last characters of a string, use the charAt() method, e.g. str. charAt(0) returns the first character, whereas str. charAt(str. length - 1) returns the last character of the string.

How do I get the first letter of a word in PHP?

So you can easily use $i[0] to fetch first letter.

How do I get the first and last character of a string in PHP?

Using substr() Method: The substr() is a built-in function in PHP that is used to extract a part of string. Example: For example, if the string is “Akshit loves GeeksForGeeks”. The last character of the string is “s”.

How do I search for a specific character in a string in PHP?

Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .