PHP How to get the first word of a string

<?php $myvalue = 'Test me more'; $arr = explode(' ',trim($myvalue)); echo $arr[0]; // will print Test ?>

1 Response

We can use the function str_word_count() this way:

<?php
$text = " Test, me more? ";
$words = str_word_count($text,1);
echo $words[0];
?>

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.