How to get the first character of a string in PHP

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.

Syntax:

substr(string,start,length)

Let’s understand the substr() function and see in how many ways we can get the characters from the string as per requirement.

<?php

echo substr("atcodex.com",10)."<br>";
echo substr("atcodex.com",1)."<br>";
echo substr("atcodex.com",3)."<br>";
echo substr("atcodex.com",7)."<br>";
echo "<br>";

// Negative numbers:
echo substr("atcodex.com",-1)."<br>";
echo substr("atcodex.com",-10)."<br>";
echo substr("atcodex.com",-8)."<br>";
echo substr("atcodex.com",-4)."<br>";
echo "<br>";

// using length
echo substr("atcodex.com",0,1)."<br>";
echo substr("atcodex.com",1,4)."<br>";
echo substr("atcodex.com",-3,1)."<br>";
echo substr("atcodex.com",4,7)."<br>";

?>

Output:

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.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *