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...
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example