How to get first n characters of a string in PHP

How to get first n characters of a string in PHP

In this post we will give you information about How to get first n characters of a string in PHP. Hear we will give you detail about How to get first n characters of a string in PHPAnd how to use it also give you demo for it if it is necessary.

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 3 etc.

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:

<?phpecho substr("Google", 0, 3);?>

Output:

"Goo"

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 start index.

Hope this code and post will helped you for implement How to get first n characters of a string in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US