Core PHP, How To, PHP Beginners

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

Written by Bikash · 1 min read >

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://codingtasks.net/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://codingtasks.net/php-online-editor/

Also Check:

Happy Coding..!

Was this article helpful?
YesNo
Written by Bikash
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. Profile

One Reply to “Get Second And Third Word from Strings in PHP”

Leave a Reply

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