Open In App

How to get the first word of a sentence in PHP?

Improve
Improve
Like Article
Like
Save
Share
Report

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 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 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 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 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


Last Updated : 07 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads