- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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, " ");
Advertisements