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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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, " ");
  
?> 

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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];
  
?> 

chevron_right


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:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
  
?> 

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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];
  
?> 

chevron_right


Output:

The first word of string is: Welcome


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.