Change language

How do I get the first word of a sentence in 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) 
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

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method