PHP Get First 5 Characters from String Example

03-Apr-2023

.

Admin

PHP Get First 5 Characters from String Example

Hi Dev,

This post is focused on php get first 5 Character from string example. Here you will learn how to get first five Character from string in php?. This tutorial will give you simple example of string to get first five Character in php example. This article will give you simple example of get first five Character from string in php example.

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

Example 1: Get First 5 Character from String


index.php

<?php

$str = "Welcome to Nicesnippets.com";

//Get First Five Character from a String

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

echo $result;

?>

Output:

Welco

Example 2: Get First 5 Character from String

index.php

<?php

$str = "Hello World";

//Get First Five Character from a String

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

echo $result;

?>

Output:

Hello

I hope it could help you...

#PHP