PHP Get First 2, 3, 4, 5, 10 Character from String Example
Hey Folks,
Now, let's see an article of php get first 2 characters of string. In this article, we will implement a php get first 3 characters of string. Here you will learn php get first 5 characters from string. you will learn php get first 10 characters of string.
we will use substr() php function to get first 2, 3, 4, 5, 10 etc character from string. i will give you very simple examples to get first 2, 3, 4, 5, 10 etc character from string in php.
Example 1: PHP Get First 2 Character from String
index.php
<?php
/* Declare string variable */
$myString = "Welcome to ItSolutionStuff.com!";
/* Get First 2 Character of string */
$firstTwo = substr($myString, 0, 2);
/* Echo resulted string */
echo $firstTwo;
?>
Output:
We
Example 2: PHP Get First 3 Character from String
index.php
<?php
/* Declare string variable */
$myString = "Welcome to ItSolutionStuff.com!";
/* Get First 3 Character of string */
$firstThree = substr($myString, 0, 3);
/* Echo resulted string */
echo $firstThree;
?>
Output:
Wel
Example 3: PHP Get First 4 Character from String
index.php
<?php
/* Declare string variable */
$myString = "Welcome to ItSolutionStuff.com!";
/* Get First 4 Character of string */
$firstFour = substr($myString, 0, 4);
/* Echo resulted string */
echo $firstFour;
?>
Output:
Welc
Example 4: PHP Get First 5 Character from String
index.php
<?php
/* Declare string variable */
$myString = "Welcome to ItSolutionStuff.com!";
/* Get First 5 Character of string */
$firstFive = substr($myString, 0, 5);
/* Echo resulted string */
echo $firstFive;
?>
Output:
Welco
Example 5: PHP Get First 10 Character from String
index.php
<?php
/* Declare string variable */
$myString = "Welcome to ItSolutionStuff.com!";
/* Get First 10 Character of string */
$firstTen = substr($myString, 0, 10);
/* Echo resulted string */
echo $firstTen;
?>
Output:
Welcome to
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 Get Last Character from String in PHP?
- How to Get First Character from String in PHP?
- How to Remove Last Character from String in PHP?
- 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?
- PHP Ajax Multiple Image Upload with Preview Example
- PHP MySQL Contact US Form with Validation Example
- PHP Import Excel File into MySQL Database Tutorial
- Convert HTML to PDF in PHP with Dompdf Example
- PHP Capture Screenshot of Website from URL Example
- PHP Remove Duplicates from Multidimensional Array Example
- How to Count Number of Files in a Directory in PHP?