w3resource
php Programming Exercies

PHP String Exercises: Get the first word of a sentence

PHP String: Exercise-18 with Solution

Write a PHP script to get the first word of a sentence.

Original String : 'The quick brown fox'

Pictorial Presentation:

PHP String Exercises: Get the first word of a sentence

Sample Solution:

PHP Code:

<?php
$s = 'The quick brown fox';
$arr1 = explode(' ',trim($s));
echo $arr1[0]."\n";
?>

Sample Output:

The

Flowchart :

Flowchart: Get the first word of a sentence

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to insert a string at the specified position in a given string.
Next: Write a PHP script to remove all leading zeroes from a string.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming