- Get first characters of a string in PHP
- Option 1: substr
- Option 2: mb_strimwidth
- PHP: Get the first character in a string.
- mb_substr and special characters.
- How to Get First Character from String in PHP?
- PHP Get First Character From String
- PHP Get Last Character From String
- PHP Get First and Last Character From String
- How to Group a Collection by Day, Month, and Year in Laravel
- How to Upgrade from Angular 15 to Angular 16?
- Related Blogs
- How to use multiple database in Laravel
- Eloquent ORM (Object Relational Mapping) in Laravel
- Eager Loading with Selected Columns in Laravel
- Implement Passport In Laravel
- How to get the first character of a string in PHP
- How to get the first character of string if we have special characters
- Conclusion:
- Related Posts
- How to Check Atleast One Checkbox is Checked or Not in JQuery?
- Laravel create custom middleware for 301 redirections to non index.php url
- Laravel middleware to remove/trim empty whitespace from the input request
Get first characters of a string in PHP
There are two primary ways to get the first characters of a string in PHP. For example, let’s say we have a description of an article but we want to trim it down to the first 30 characters to generate an excerpt.
Option 1: substr
To me, the easiest option is to use PHP’s substr function. Therefore, substr is my preferred method of getting the first characters of a string in PHP.
Per PHP’s documentation, the substr function allows you to return part of a string. The code example they provide is listed below:
substr ( string $string , int $start [, int $length ] ) : string
So, in the function below, 0 is the position where you want to start from and 30 is the number of characters that you would like to show from the start of the string.
$first_part_of_string = substr($string,0,30);
For example, in order to get the first 30 characters of our article description, we would run the following code. As you see below, I prefer to trim the description before running it through substr so that the result won’t have spaces at the beginning or end and I want to add an ellipsis at the end so my users know that there is more to read:
$description = 'This article is all about getting the first part of a string in PHP. We show you different PHP functions that get you there as quickly as possible. '; $excerpt = substr(trim($description),0,30).'. '; // this generates the following string - This article is all about gett. echo $excerpt;
Option 2: mb_strimwidth
A second way to return the first characters of a string in PHP is to use the mb_strimwidth function. This is listed as the second option because I don’t use it as regularly as I use the substr when I code, but it’s just as easy to use.
Per PHP’s documentation, the mb_strimwidth function allows you to get a truncated string with a specified width. A code example that they provide is visible below:
mb_strimwidth ( string $str , int $start , int $width [, string $trimmarker = "" [, string $encoding = mb_internal_encoding() ]] ) : string
In order to go from the start of the string to the following 30 characters, you need to replace int $start with 0 and replace int $width with 30. This looks like the following function:
$first_part_of_string = mb_strimwidth($string, 0, 30, '. ');
For our specific example, we want to return the excerpt of an article description. Again, I use the trim function to trim spaces from the beginning or end of the returned string and I use an ellipsis to show my users that there is more to read.
$description = 'This article is all about getting the first part of a string in PHP. We show you different PHP functions that get you there as quickly as possible. '; $excerpt = mb_strimwidth(trim($description), 0, 30).'. '; // this generates the following string - This article is all about gett. echo $excerpt;
Add Comments to YAML File
Customize Style of Google Translate Widget
PHP: Get the first character in a string.
This is a short guide on how to get the first character of a string in PHP. As an added bonus, I will also show you how to get the first two characters of a string.
We used two different approaches here.
Using the first approach, we treat the string like an array. i.e. We use the index position of the character to access it. This is possible because strings in PHP can be treated like character arrays (basically, a string is just an array that consists of characters).
In the second approach, we used the PHP function substr, which returns a part of a string. With the substr function, there are three parameters:
- The string in question.
- The index to start at (we set this to 0 because we want to start at the very beginning of the string).
- The length that we want to return (we set this to 1 because we only want the first character).
mb_substr and special characters.
If there is a chance that your string contains special characters such as “ö” or “ë”, then you will need to use the mb_substr function. This function is the multi-byte safe version of substr. If you use the substr on a string that contains special characters, then you might end up with jumbled characters and / or the black diamond question mark “replacement character”.
Example of mb_substr being used to get the first character of a string that contains special characters:
//Set the header to utf-8 for example purposes. header('Content-Type: text/html; charset=utf-8'); //Example string. $string = 'öëBC'; //Use mb_substr to get the first character. $firstChar = mb_substr($string, 0, 1, "UTF-8"); //Print out the first character. echo $firstChar;
Note that this function allows you to specify the encoding as a fourth parameter!
Anyway; hopefully you found this short tutorial helpful!
How to Get First Character from String in PHP?
In this article, we will implement how to get the first character from a string in PHP.
Smit Pipaliya
In this quick example, PHP gets the first character from the string. In this article, we will implement how to get the first character from a string in PHP. If you have a php question, get the first character from the string example, then I will give a simple example with a solution. I explained to get to the first character from string PHP.
We will use the substr() PHP function to get the first character from the string. I will give you very simple examples to get the first character from a string in PHP.
PHP Get First Character From String
PHP Get Last Character From String
PHP Get First and Last Character From String
Thank you for reading this article.
If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.
How to Group a Collection by Day, Month, and Year in Laravel
How to Upgrade from Angular 15 to Angular 16?
Related Blogs
How to use multiple database in Laravel
Laravel allows connecting multiple databases with different database engines. Laravel has inbuild support for multiple database systems, you need to provide connection detail in their database config file.
Eloquent ORM (Object Relational Mapping) in Laravel
The Eloquent ORM Provides an ActiveRecord Implementation to work with your database. Eloquent ORM seems like a simple mechanism, but under the hood, there are a lot of semi-hidden functions.
Eager Loading with Selected Columns in Laravel
Eager Loading is a part of laravel relationship and it is the best. But sometimes we just need to get specific columns from the relation model.
Implement Passport In Laravel
Laravel Passport is a native OAuth 2 server for Laravel apps. Like Cashier and Scout, you’ll bring it into your app with Composer.
How to get the first character of a string in PHP
Many times in the application we required to add or remove some part of the string for use. So in this tutorial, we will learn how to get the first characters of a string in PHP. For example, we have to take the first 5 characters from the string to show in the website, or we need to show string first character in the web application. In this scenario, we have to use substr() function which gives options to manipulate the string as per need.
Let’s understand the substr() function and see in how many ways we can get the characters from the string as per requirement.
"; echo substr("atcodex.com",1)."
"; echo substr("atcodex.com",3)."
"; echo substr("atcodex.com",7)."
"; echo "
"; // Negative numbers: echo substr("atcodex.com",-1)."
"; echo substr("atcodex.com",-10)."
"; echo substr("atcodex.com",-8)."
"; echo substr("atcodex.com",-4)."
"; echo "
"; // using length echo substr("atcodex.com",0,1)."
"; echo substr("atcodex.com",1,4)."
"; echo substr("atcodex.com",-3,1)."
"; echo substr("atcodex.com",4,7)."
"; ?>
m tcodex.com odex.com .com m tcodex.com odex.com .com a tcod c dex.com
In the above example, you can see, we have passed 3 parameters to the function. first is the string second is the start position and the third is the length. If we take the second value in positive the characters will be counted from the left and if we take the second value negative then the characters will be counted from the right end of the string. We have provided the different scenarios in the above example, test them by changing the value of second and third value in the function and manipulate the result.
How to get the first character of string if we have special characters
substr() function work will with normal string, but when we have special characters in the string then it will not give correct output.
substr() function is not able to read the special character from the string. So in that case PHP provides us the other function called mb_substr() function, which has the almost the same functionality as substr() has, but the only difference is that mb_sbustr() can work with special character as it has the option to pass the encoding format in the function.
To get the first character of a string in PHP, you can use a built-in function i.e. substr(). But if you have special characters in the strings then it will show you a black symbol or some unreadable symbol rather than to show that special character as an output.
NOTE: mb_substr is used to get the character of a string that contains special characters:
Conclusion:
We have tried to explain the possible scenario in which you can understand the different ways to manipulate the string using substr() function. Hope you liked the post. You can also check How to Remove Last Character from String in PHP in which have explained all the possible methods and ways to manipulate the string and how to get any part of the string using 4 different methods.
Related Posts
How to Check Atleast One Checkbox is Checked or Not in JQuery?
October 7, 2021 October 10, 2021
Laravel create custom middleware for 301 redirections to non index.php url
October 6, 2021 October 8, 2021
Laravel middleware to remove/trim empty whitespace from the input request
October 5, 2021 October 5, 2021