How to Get First and Last Character from String in PHP?
Hello Folks,
This tutorial will provide an example of php get first and last character from string. This example will help you how to get first and last character from string in php. This example will help you php get first and last character from string example. if you want to see an example of get first and last character from string php then you are in the right place.
we will use substr() php function to get first and last character from string. i will give you very simple examples to get first and last character from string in php.
Example 1: PHP Get First and Last Character from String
index.php
<?php
/* Declare string variable */
$myString = "Welcome to ItSolutionStuff.com!";
/* get First and Last Character of string */
$last = substr($myString, 0, 1);
$last = substr($myString, -1);
/* Echo resulted string */
echo $last;
echo $last;
?>
Output:
W
!
Example 2: PHP Get Last Character from String
index.php
<?php
/* Declare string variable */
$myString = "Welcome to ItSolutionStuff.com!";
/* get Last Character of string */
$char = substr($myStr, -1);
/* Echo resulted string */
echo $char;
?>
Output:
!
Example 3: PHP Get First Character from String
index.php
<?php
/* Declare string variable */
$myString = "Welcome to ItSolutionStuff.com!";
/* get First Character of string */
$char = substr($myString, 0, 1);
/* Echo resulted string */
echo $char;
?>
Output:
W
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Remove First Character from String in PHP?
- PHP Remove First and Last Character from String Example
- PHP Remove Whitespace from Beginning and End of String Example
- How to Check Query Execution Time in PHP?
- How to Remove Special Characters from String in PHP?
- PHP JQuery Ajax Image Upload Example Tutorial
- Multiple File Upload using Dropzone JS in PHP Example
- PHP Import Excel File into MySQL Database Tutorial
- How to Convert Object into Array in PHP?
- PHP Download File from URL using CURL Request Example
- How to Count Number of Files in a Directory in PHP?
- How to Find Day Name from Specific Date in PHP?
- How to Remove Null Values from Array in PHP?