Home » Get Second And Third Word from Strings in PHP

Get Second And Third Word from Strings in PHP

To Get Second And Third Word from Strings in PHP we use PHP’s inbuilt function named as explode() function. Before we check the source code to get any word from any position using PHP, we do some introduction about PHP explode function.

Get Second And Third Word from Strings in PHP

Introduction to Explode Function in PHP

The explode() function breaks the given string into the array.

Syntax of Explode function

explode(separator,string,limit)

  • Separator must not be blank.
  • Any type of string.
  • Limit of an array element according to your string.

Example:

Array ( [0] => php,coder,tech,explode )

Array ( [0] => php [1] => coder,tech,explode )

To know more about PHP explode function you can check here: https://phpcoder.tech/php-explode-function/

Now we check how to get Second And Third Word from Strings in PHP,

Source code to get Second And Third Word from Strings in PHP

Output:

PHP Get the First 5 Words From String

Output:

Lorem ipsum dolor sit amet

Code Explanations:

  • We use str_word_count() function to check an array where the key is the position of the word in the string, and the value is the actual word.
  • array_slice() it returns 1 to 5 words from the given string.

Check the complete use and explanations of explode function from the PHP official site, https://www.php.net/manual/en/function.explode.php

You can check all the code using our online compiler, https://phpcoder.tech/php-online-editor/

Also Check:

Happy Coding..!

1 thought on “Get Second And Third Word from Strings in PHP”

  1. Pingback: Convert String to Array in PHP Without Explode - PHPCODER.TECH

Leave a Reply

Your email address will not be published. Required fields are marked *