How do I get the first word of a sentence in PHP?

PHP

Method 1: Using the function strtok() :this function is used to splitting the string into smaller parts based on the specified delimiters. It takes an input string as an argument along with delimiters (as the second argument).Syntax:
string strtok ($string, $delimiters) < / pre> Program :      
// PHP program for getting the first word
// suggestion using strtok() function 
// Initialize the sentence to a variable $sentence = `Welcome to GeeksforGeeks` ;  
// Use the strtok() function to get
// first word of line echo " The first word of string is: " . strtok ( $sentence , " " );  
?>
Exit:
The first word of string is: Welcome
Method 2. Using trim() and explode() functions:
  • The trim() function :The trim() function is used to remove spaces as well as predefined characters from both left and right sides of a string. Syntax:
    trim ($string, $charlist)
  • The explode() function :The explode() function is used to split a string into different lines. The explode() function splits the string based on the string delimiter, that is, it splits the string wherever the delimiter character is. This function returns an array containing strings formed by splitting the original string. Syntax:
    array explode (separator, OriginalString, NoOfElements)
Program :
// PHP program to get the first word
// sentence using trim() and
// explosion function() 
// Initialize the sentence in variable $sentence = `Welcome to GeeksforGeeks` ;  
// Use trim() and explode() functions for
// get the first word of the line $arr = explode ( ` ` , trim ( $sentence )); echo " The first word of string is: " . $arr [0];  
?>
Exit:
The first word of string is: Welcome
Method 3: Using the function strstr() : The strstr() function is used to find the first occurrence of a string in another string. This function is case sensitive.Syntax:
strstr ($string, $search, $before)
Program:
// PHP program for getting the first word
// Suggestion using strstr() function 
// Initialize the sentence to a variable $sentence = `Welcome to GeeksforGeeks` ;  
// Use the strstr() function to get
// first word of the line echo "The first word of string is: " . strstr ( $sentence , ` ` , true);  
?>
Exit:
The first word of string is: Welcome
Method 4: Using preg_match() Function : This function looks for a pattern in a string, returns true if the pattern exists, otherwise returns false. The search usually starts at the beginning of the subject line. The optional offset parameter is used to specify the position where the search starts.Syntax:
int preg_match ($pattern, $input, $matches, $flags , $offset)
Program :
// PHP program for getting the first word
// Suggestion using preg_match() function 
// Initialize the sentence to a variable $sentence = ` Welcome to GeeksforGeeks` ;  
// Use preg_match() to get
// first word of the line preg_match ( ` / w + / i` , $sentence , $result );  
// Show result echo "The first word of string is:" . $result [0];  
?>
Exit:
The first word of string is: Welcome




Get Solution for free from DataCamp guru