PHP Get First and Last Word from String Example

03-Apr-2023

.

Admin

PHP Get First and Last Word from String Example

Hi Dev,

In this example, you will learn php get first and last word from string example. if you want to see example of how to get first and last word from string in php? then you are a right place. We will use get first and last word from string in php example. We will use how to use function to get first and last word from string in php? .

In this example to get first and last word from string in PHP. in this example, we will use to explode() function to get first and last word. so Let's see the following example with output.

Example 1: Get First and Last Word from String


index.php

<?php

$str = "Hello Jaydeep and Nikhil";

// Get Frist and Last Word

$array = explode(" ",$str);

$first_word = $array[0];

$last_word = $array[count($array)-1];

echo $first_word. ', '.$last_word;

?>

Output:

Hello, Nikhil

I hope it could help you...

#PHP