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

Subscribe For Latest Updates

Sign up to best of PHP Code Solutions, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.

Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.

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

Leave a Reply

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