Computer >> Computer tutorials >  >> Programming >> PHP

PHP program to find the first word of a sentence


To find the first word of a sentence, the PHP code is as follows −

Example

<?php
   $my_string = 'Hi there, this is a sample statement';
   echo "The first word of the string is ". strtok($my_string, " ");
?>

Output

The first word of the string is Hi

A string is defined at first −

$my_string = 'Hi there, this is a sample statement';

The ‘strtok’ function is an in-built function that is used to split a string into specified number of parts. Before the first space occurs, the string needs to be split. This split string’s first part is displayed as the output on the screen −

echo "The first word of the string is ". strtok($my_string, " ");