ItSolutionStuff.com

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

By Hardik Savani • May 14, 2024
PHP

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...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Last Character from String in PHP?

Read Now →

How to Get First Character from String in PHP?

Read Now →

How to Remove Last Character from String in PHP?

Read Now →

How to Remove First Character from String in PHP?

Read Now →

PHP Remove First and Last Character from String Example

Read Now →

PHP Remove Whitespace from Beginning and End of String Example

Read Now →

How to Check Query Execution Time in PHP?

Read Now →

PHP Ajax Multiple Image Upload with Preview Example

Read Now →

PHP MySQL Contact US Form with Validation Example

Read Now →

PHP Import Excel File into MySQL Database Tutorial

Read Now →

Convert HTML to PDF in PHP with Dompdf Example

Read Now →

PHP Capture Screenshot of Website from URL Example

Read Now →

PHP Remove Duplicates from Multidimensional Array Example

Read Now →

How to Count Number of Files in a Directory in PHP?

Read Now →