PHP Get First Word from String Example
Hi Dev,
This tutorial is focused on php get first word from string example. I’m going to show you about how to get first word from string in php?. I would like to share with you get first word from string in php example. you'll learn how to use function to get first word from string in php? .
There are tow example to get First word from strings in PHP. in this example, we will use to strtok() and preg_match() function to remove First word. so Let's both the following example with output.
Example 1: Get First Word from String
index.php
<?php
$str = "Welcome to Nicesnippets.com";
//Get first word of a string
$result = strtok($str, " ");
echo $result;
?>
Output:
Welcome
Example 2: Get First Word from String
index.php
<?php
$str = 'Hello all Student';
//Get first word of a string
preg_match('/\b\w+\b/i', $str, $result);
echo $result[0];
?>
Output:
Hello
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