How to get first n characters of a string in PHP
In this tutorial, we are going to learn about how to get the first n characters of a string in PHP.
In PHP, strings are the sequence of characters, where the index of the first character is
0, the second character is
1, third character is
Get the first n characters of a string
To get the first n characters of a string, we can use the built-in substr() function in PHP.
Here is an example, that gets the first 3 characters from a following string:
<?php echo substr("Google", 0, 3); ?>
In the example above, we have passed
0, 3 as an arguments to the
substr() function. So, it begins the extraction at index
0 and extracts before the 3rd character of a string.
Syntax : substr(string, start, length)
The second argument is
start index and third argument is
length that means how many characters you need to get from the