How to display first 100 characters of a string in PHP

All QuestionsCategory: PHPHow to display first 100 characters of a string in PHP
S M Hasibul Islam Staff asked 4 years ago

I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.
Is there a function that can do this easily?
For example:

$string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.";
$string2 = 100charfunction($string1);
print $string2

To get: I am looking for a way to pull the first 100 characters from a string vari

1 Answers
S M Hasibul Islam Staff answered 4 years ago

You could use substr, I guess:

$string2 = substr($string1, 0, 100);

or mb_substr for multi-byte strings:

$string2 = mb_substr($string1, 0, 100);