PHP Get First 2, 3, 4, 5, 10 Character from String Example - TechvBlogs
PHP

PHP Get First 2, 3, 4, 5, 10 Character from String Example

In this article, We will learn How to get the first 2, 3, 4, 5, and 10 Characters from a String in PHP with Examples.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

10 months ago

TechvBlogs - Google News

Now, let's see an article of php get the first 2 characters of the string. In this article, we will implement a PHP to get the first 3 characters of the string. You will learn PHP to get the first 5 characters from a string here. You will learn PHP to get the first 10 characters of the string.

We will use the substr() PHP function to get the first 2, 3, 4, 5, and 10 characters from the string. I will give you very simple examples to get the first 2, 3, 4, 5, 10, etc. characters from the strings in PHP.

PHP Get First 2 Characters from String

<?php
    
    /* Declare string variable */
    $myString = "Welcome to TechvBlogs.com!";
         
    /* Get First 2 Character of string */
    $firstTwo = substr($myString, 0, 2);
         
    /* Echo resulted string */
    echo $firstTwo;
    
?>

Output:

We

PHP Get the First 3 Characters from String

<?php
    
    /* Declare string variable */
    $myString = "Welcome to TechvBlogs.com!";
         
    /* Get First 2 Character of string */
    $firstTwo = substr($myString, 0, 3);
         
    /* Echo resulted string */
    echo $firstTwo;
    
?>

Output:

Wel

PHP Get the First 4 Characters from String

<?php
    
    /* Declare string variable */
    $myString = "Welcome to TechvBlogs.com!";
         
    /* Get First 2 Character of string */
    $firstTwo = substr($myString, 0, 4);
         
    /* Echo resulted string */
    echo $firstTwo;
    
?>

Output:

Welc

PHP Get the First 5 Characters from String

<?php
    
    /* Declare string variable */
    $myString = "Welcome to TechvBlogs.com!";
         
    /* Get First 2 Character of string */
    $firstTwo = substr($myString, 0, 5);
         
    /* Echo resulted string */
    echo $firstTwo;
    
?>

Output:

Welco

PHP Get First 10 Characters from String

<?php
    
    /* Declare string variable */
    $myString = "Welcome to TechvBlogs.com!";
         
    /* Get First 2 Character of string */
    $firstTwo = substr($myString, 0, 10);
         
    /* Echo resulted string */
    echo $firstTwo;
    
?>

Output:

Welcome to

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.

PHP

Comments (0)

Comment


Note: All Input Fields are required.