PHP Get First 2 Character from String Example

03-Apr-2023

.

Admin

PHP Get First 2 Character from String Example

Hi Dev,

In this example, you will learn php get first 2 Character from string example. step by step explain how to get first two Character from string in php?. This tutorial will give you simple example of get first two Character from string in php example. This article will give you simple example of how to use function to get first two Character from string in php? .

There are tow example to get First 2 Character from strings in PHP. in this example, we will use to substr() and mb_substr() function to get First 2 Character. so Let's both the following example with output.

Example 1: Get First 2 Character from String


index.php

<?php

$str = "Hello World";

//Get first Two character from a string

$result = substr($str,0,2);

echo $result;

?>

Output:

He

Example 2: Get First 2 Character from String

index.php

<?php

$str = "Welcome to Nicesnippets.com";

//Get first Two character from a string

$result = mb_substr($str,0,2);

echo $result;

?>

Output:

We

I hope it could help you...

#PHP