
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the First Word of a Sentence in PHP
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, " ");
Advertisements