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

By Hardik Savani May 14, 2024 Category : 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 :
Shares