How to get the first word of a sentence in PHP?
Last Updated :
11 Jul, 2025
The first word of a sentence can be obtained with the help of various inbuilt functions like strtok(), strstr(), explode() and some other methods like using regular expressions. Some examples to get the first word of a sentence are listed below:
Method 1: Using strtok() Function: This function is used to tokenize a string into smaller parts on the basis of given delimiters. It takes input String as an argument along with delimiters (as second argument).
Syntax:
string strtok( $string, $delimiters )
Program:
php
<?php
// PHP program to get the first word of
// a sentence using strtok() function
// Initialize a sentence to a variable
$sentence = 'Welcome to GeeksforGeeks';
// Use strtok() function to get the
// first word of a string
echo "The first word of string is: "
. strtok($sentence, " ");
?>
Output:
The first word of string is: Welcome
Method 2: Using trim() and explode() Function:
- trim() Function: The trim() function is used to remove the whitespaces and also the predefined characters from both sides of a string that is left and right.
Syntax:
trim( $string, $charlist )
- explode() Function: The explode() function is used to split a string in different strings. The explode() function splits a string based on the string delimiter, i.e. it splits the string wherever the delimiter character occurs. This function returns an array containing the strings formed by splitting the original string.
Syntax:
array explode( separator, OriginalString, NoOfElements )
Program:
php
<?php
// PHP program to get the first word of
// a sentence using trim() and
// explode()function
// Initialize a sentence to a variable
$sentence = 'Welcome to GeeksforGeeks';
// Use trim() and explode() function to
// get the first word of a string
$arr = explode(' ', trim($sentence));
echo "The first word of string is: " . $arr[0];
?>
Output:
The first word of string is: Welcome
Method 3: Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string. This function is case-sensitive.
Syntax:
strstr( $string, $search, $before )
Program:
php
<?php
// PHP program to get the first word of
// a sentence using strstr() function
// Initialize a sentence to a variable
$sentence = 'Welcome to GeeksforGeeks';
// Use strstr() function to get the
// first word of a string
echo "The first word of string is: "
. strstr($sentence, ' ', true);
?>
Output:
The first word of string is: Welcome
Method 4: Using preg_match() Function: This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.
Syntax:
int preg_match( $pattern, $input, $matches, $flags, $offset )
Program:
php
<?php
// PHP program to get the first word of
// a sentence using preg_match() function
// Initialize a sentence to a variable
$sentence = 'Welcome to GeeksforGeeks';
// Use preg_match() function to get the
// first word of a string
preg_match('/\b\w+\b/i', $sentence, $result);
// Display result
echo "The first word of string is: ".$result[0];
?>
Output:
The first word of string is: Welcome
Similar Reads
How to get the First Element of an Array in PHP? Accessing the first element of an array in PHP is a common task, whether they are indexed, associative, or multidimensional. PHP provides various methods to achieve this, including direct indexing, and built-in functions.Below are the approaches to get the first element of an array in PHP:Table of C
4 min read
How to get the First Element of an Array in PHP? Accessing the first element of an array in PHP is a common task, whether they are indexed, associative, or multidimensional. PHP provides various methods to achieve this, including direct indexing, and built-in functions.Below are the approaches to get the first element of an array in PHP:Table of C
4 min read
How to get the First Element of an Array in PHP? Accessing the first element of an array in PHP is a common task, whether they are indexed, associative, or multidimensional. PHP provides various methods to achieve this, including direct indexing, and built-in functions.Below are the approaches to get the first element of an array in PHP:Table of C
4 min read
How to count the number of words in a string in PHP ? Given a string containing some words and the task is to count number of words in a string str in PHP. In order to do this task, we have the following approaches:Table of ContentUsing str_word_count() MethodUsing trim(), preg_replace(), count() and explode() method. Using trim(), substr_count(), and
4 min read
How to get the last n characters of a PHP string? Write a PHP program to get last n characters of a given string. Examples: Input : $str = "GeeksforGeeks!" $n = 6 Output : Geeks! Input : $str = "GeeksforGeeks!" $n = 9 Output : forGeeks! Method 1: Tn this method, traverse the last N characters of the string and keep appending them in a new string. E
1 min read
How to replace a word inside a string in PHP ? Given a string containing some words the task is to replace all the occurrences of a word within the given string str in PHP. To do this task, we have the following methods in PHP:Table of ContentUsing str_replace() MethodUsing str_ireplace() Method Using preg_replace() MethodUsing strtr()Using preg
4 min read