PHP Get First 10 Word from String Example

03-Apr-2023

.

Admin

PHP Get First 10 Word from String Example

Hi Dev,

In this tutorial, I will show you php get first 10 word from string example. it's simple example of how to get first 10 word from string in php?. This post will give you simple example of get first 10 word from string in php example. I explained simply about how to use function to get first 10 word from string in php? .

There are tow example to get First 10 word from strings in PHP. in this example, we will use to the first example explode(), array_slice() and implode() function and second example to use str_word_count() function to get First 10 word. so Let's both the following example with output.

Example 1: Get First ten Word from String


index.php

<?php

$str = "This Website to Very Useful for Developer and early learning web development.";

$words = explode(' ', $str);

// Get First ten (10) words from String

$five_words = array_slice($words,0,10);

$result = implode(' ',$five_words);

echo $result;

?>

Output:

This Website to Very Useful for Developer and early learning

Example 2: Get First ten Word from String

index.php

<?php

$str = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod

tempor incididunt ut labore et dolore magna aliqua.';

// Get First Ten (10) words from String

$result = implode(' ', array_slice(str_word_count($str, 2), 0, 10));

echo $result;

?>

Output:

Lorem ipsum dolor sit amet consectetur adipisicing elit sed do

I hope it could help you...

#PHP