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
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..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.
Posts created 188

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

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top