PHP program to find the first word of a sentence

PHPServer Side ProgrammingProgramming

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

Example

Live Demo

<?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, " ");
Updated on 17-Aug-2020 11:57:19