In this article, we will explain how to get first character from string in PHP. If you have a question about how to extract the first character from a string, I will provide a simple example with a solution.
We will use the ‘substr()’ function in PHP to get first character from string.
1. Get first character from string in PHP using substr() function
The substr() function is used to extract a portion of a string. It allows you to specify the starting position and the length of the substring you want retrieve. To me simple option is to use PHP’s substr function. substr function is preferred method of getting the first character of a string in PHP.
Syntax:
We will use the ‘substr()’ function to get the first character from a string and explain the syntax with an example.
substr(string $string, int $start [, int $length]) : string
Return the portion of string specified by the offset and length parameters.
Parameters
- $string: The input string from which is substring will be extracted.
- $start: The starting position for extract.
- If positive, it starts counting from the beginning of the string.
- If negative, it starts counting from the end of the string.
- $length: The number if characters to extract.
<?php
$my_string = "Welcome to Webstuffsolution!";
$char_new = substr($my_string, 0, 1);
echo $char_new; // Output: W
?>
Explain:
- Starts at position 0 (Start character)
- Extract 1 character.
Read also: How to convert string to array in PHP?
2. PHP Get first letter of string in PHP using str_split() function
We will use the ‘str_split()’ function in PHP to get the first character of a string. The ‘str_split()’ function splits a string into an array of characters.
- You need to process or iterate over each character or fixed-length chunk of the string.
- You want to manipulate or analyze individual characters or groups of character the string.
Syntax:
str_split(string $string, int $length = 1): array
By default, the length is set to 1, which converts the string into an array of single characters. However, if the length set to 3, it will group the string into an array where each element contains 3 characters starting from the specific index.
<?php
$my_string = "Welcome to Webstuffsolution!";
$new_array = str_split($my_string);
$char_new = $new_array[0];
echo $char_new; // Output: W
?>
3. Get first character from string in PHP using string indexing
Example
You can directly access the first character of a string using string indexing. It is a easy method to get first character from string in php. String can be seen as character array, and the method to access a position of an array is to use the [] operator.There is no problem at all in using $my_string[0].
<?php
$my_string = "Welcome to Webstuffsolution!";
$firstCharacter = $my_string[0];
echo $firstCharacter; // Output: W
?>
These examples provide a variety of methods to get the first letter of a string in PHP, covering both simple and more complex scanarios.
4. PHP get first character of string using mb_substr() function
This is preferred method when dealing with multibyte strings(UTF-8 encoded text), such as non ASCII characters, to avoid incorrect slicing character. This is important if you’re using multibyte encoding. if you want to support that, use mb_substr().goog
Syntax:
mb_substr($string, $start, $length, $encoding);
<?php
$my_string = "नमस्ते" // ;
$firstCharacter = mb_substr($my_string, 0, 1, 'UTF-8');
echo $firstCharacter; // Output: न
?>
- $encoding: Optional parameter to specify character encoding. (UTF-8)
5. Handling empty strings
It’s important to handle where the string might be empty to avoid errors. This function checks of the string is empty. While this doesn’t directly extract a character.
<?php
$my_string = "";
if(!empty($my_string)) {
$first_character = $my_string[0];
echo "First character of '$my_string' is: $first_character";
}else {
echo "String is empty.";
}
?>
6. Get first character of string using preg_match() method
You can use a regular expression to match the first character of the string. This function allows using regular expression to extract specific parts of a string.
Syntax:
preg_match($pattern, $string, $matches);
<?php
$my_string = "Welcome to Webstuffsolution!";
$data = preg_match('/^./', $my_string, $matches);
$firstCharacter = $data[0];
echo $firstCharacter; // Output: W
?>
Explanation:
substr(): Common and simple for general use.
mb_substr(): Prefered for working with multibyte strings.
str_split(): Useful if you need to work with the entire string in array.
empty(): This method check to string is empty or not.
preg_match(): Adds flexibility, thought for the simple extraction.
7. PHP get length of characher using mb_strimwidth
A that method to return the first character of a string in PHP is to use the mb_strimwidth function. This is as the option but don’t use regularly as i use the substr when it is just as easy to use.
Syntax:
mb_strimwidth( string $str, int $strat, int $width [, string
$trimmarker = "" [, string $encoding = mb_internal_encoding()]]) :string
In order to go from the start of the string to the following characters. You can set the length as many strings as you need to get.
$data = "Welcome to webstuffsolution.com";
$excerptstring = mb_strimwidth(trim($data), 0, 1);
echo $excerptstring;
In PHP 8 if you just need to check if the first character of a given string is equal to something, you can use like this
$checkString = "W";
$data = "Welcome to webstuffsolution.com";
if(str_starts_with($data, $checkString)) {
// DO SOMETHING
}
I hope this tutorial help you.