How to get the first word of a sentence in PHP?

PhpStringExtractText Segmentation

Php Problem Overview


I want to extract the first word of a variable from a string. For example, take this input:

<?php $myvalue = 'Test me more'; ?>

The resultant output should be Test, which is the first word of the input. How can I do this?

Php Solutions


Solution 1 - Php

There is a string function (strtok) which can be used to split a string into smaller strings (tokens) based on some separator(s). For the purposes of this thread, the first word (defined as anything before the first space character) of Test me more can be obtained by tokenizing the string on the space character.

<?php
$value = "Test me more";
echo strtok($value, " "); // Test
?>

For more details and examples, see the strtok PHP manual page.

Solution 2 - Php

You can use the explode function as follows:

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

Another example:

$sentence = 'Hello World this is PHP';
$abbreviation = explode(' ', trim($sentence ))[0];
echo $abbreviation // will print Hello

Solution 3 - Php

If you have PHP 5.3

$myvalue = 'Test me more';
echo strstr($myvalue, ' ', true);

note that if $myvalue is a string with one word strstr doesn't return anything in this case. A solution could be to append a space to the test-string:

echo strstr( $myvalue . ' ', ' ', true );

That will always return the first word of the string, even if the string has just one word in it

The alternative is something like:

$i = strpos($myvalue, ' ');
echo $i !== false ? $myvalue : substr( $myvalue, 0, $i );

Or using explode, which has so many answers using it I won't bother pointing out how to do it.

Solution 4 - Php

You could do

echo current(explode(' ',$myvalue));

Solution 5 - Php

Even though it is little late, but PHP has one better solution for this:

$words=str_word_count($myvalue, 1);
echo $words[0];

Solution 6 - Php

Similar to accepted answer with one less step:

$my_value = 'Test me more';
$first_word = explode(' ',trim($my_value))[0];

//$first_word == 'Test'

Solution 7 - Php

Just in case you are not sure the string starts with a word...

$input = ' Test me more ';
echo preg_replace('/(\s*)([^\s]*)(.*)/', '$2', $input); //Test

Solution 8 - Php

<?php
  $value = "Hello world";
  $tokens = explode(" ", $value);
  echo $tokens[0];
?>

Just use explode to get every word of the input and output the first element of the resulting array.

Solution 9 - Php

Using split function also you can get the first word from string.

<?php
$myvalue ="Test me more";
$result=split(" ",$myvalue);
echo $result[0];
?>

Solution 10 - Php

$string = ' Test me more ';
preg_match('/\b\w+\b/i', $string, $result); // Test
echo $result;

/* You could use [a-zA-Z]+ instead of \w+ if wanted only alphabetical chars. */
$string = ' Test me more ';
preg_match('/\b[a-zA-Z]+\b/i', $string, $result); // Test
echo $result;

Regards, Ciul

Solution 11 - Php

strtok is quicker than extract or preg_* functions.

Solution 12 - Php

$input = "Test me more";
echo preg_replace("/\s.*$/","",$input); // "Test"

Solution 13 - Php

personally strsplit / explode / strtok does not support word boundaries, so to get a more accute split use regular expression with the \w

preg_split('/[\s]+/',$string,1);

This would split words with boundaries to a limit of 1.

Solution 14 - Php

If you want to know how fast each of these respective functions is, I ran some crude benchmarking in PHP 7.3 on the six most voted answers here (strpos with substr, explode with current, strstr, explode with trim, str_word_count and strtok) with 1,000,000 iterations each to compare their speeds.

<?php

$strTest = 'This is a string to test fetching first word of a string methods.';

$before = microtime(true);
for ($i=0 ; $i<1000000 ; $i++) {
    $p = strpos($strTest, ' ');
    $p !== false ? $strTest : substr( $strTest, 0, $p );
}
$after = microtime(true);
echo 'strpos/ substr: '.($after-$before)/$i . ' seconds<br>';

$before = microtime(true);
for ($i=0 ; $i<1000000 ; $i++) {
    strstr($strTest, ' ', true);
}
$after = microtime(true);
echo 'strstr: '.($after-$before)/$i . ' seconds<br>';

$before = microtime(true);
for ($i=0 ; $i<1000000 ; $i++) {
    current(explode(' ',$strTest));
}
$after = microtime(true);
echo 'explode/ current: '.($after-$before)/$i . ' seconds<br>';

$before = microtime(true);
for ($i=0 ; $i<1000000 ; $i++) {
    $arr = explode(' ',trim($strTest));
    $arr[0];
}
$after = microtime(true);
echo 'explode/ trim: '.($after-$before)/$i . ' seconds<br>';

$before = microtime(true);
for ($i=0 ; $i<1000000 ; $i++) {
    str_word_count($strTest, 1);
}
$after = microtime(true);
echo 'str_word_count: '.($after-$before)/$i . ' seconds<br>';

$before = microtime(true);
for ($i=0 ; $i<1000000 ; $i++) {
    strtok($value, ' ');
}
$after = microtime(true);
echo 'strtok: '.($after-$before)/$i . ' seconds<br>';

?>

Here are the varying results from 2 consecutive runs:

strpos/ substr: 6.0736894607544E-8 seconds
strstr: 5.0434112548828E-8 seconds
explode/ current: 3.5163116455078E-7 seconds
explode/ trim: 3.8683795928955E-7 seconds
str_word_count: 4.6665270328522E-6 seconds
strtok: 4.9849510192871E-7 seconds

strpos/ substr: 5.7171106338501E-8 seconds
strstr: 4.7624826431274E-8 seconds
explode/ current: 3.3753299713135E-7 seconds
explode/ trim: 4.2293286323547E-7 seconds
str_word_count: 3.7025549411774E-6 seconds
strtok: 1.2249300479889E-6 seconds

And the results after inverting the order of the functions:

strtok: 4.2612719535828E-7 seconds
str_word_count: 4.1899878978729E-6 seconds
explode/ trim: 9.3175292015076E-7 seconds
explode/ current: 7.0811605453491E-7 seconds
strstr: 1.0137891769409E-7 seconds
strpos/ substr: 1.0082197189331E-7 seconds

Conclusion It turns out that the speed between these functions varies widely and is not as consistent between test runs as you might expect. According to these quick and dirty tests, any of the six chosen functions will get the job done in a reasonable amount of time. There are perturbations including other processes running that are interfering with the execution times. So just use whatever function makes the most practical and readable sense to you as a programmer. For the bigger programming picture, see Donald Knuth's Literate Programming.

Solution 15 - Php

$first_word = str_word_count(1)[0]

Doesn't work on special characters, and will result in wrong behaviour if special characters are used. It is not UTF-8 friendly.

For more info check https://stackoverflow.com/questions/8290537/is-php-str-word-count-multibyte-safe

Solution 16 - Php

You question could be reformulated as "replace in the string the first space and everything following by nothing" . So this can be achieved with a simple regular expression:

$firstWord = preg_replace("/\s.*/", '', ltrim($myvalue));

I have added an optional call to ltrim() to be safe: this function remove spaces at the begin of string.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaliView Question on Stackoverflow
Solution 1 - PhpsalatheView Answer on Stackoverflow
Solution 2 - PhpcodaddictView Answer on Stackoverflow
Solution 3 - PhpYacobyView Answer on Stackoverflow
Solution 4 - PhpAntonioCSView Answer on Stackoverflow
Solution 5 - PhpLakshmi Rawat SinghaniaView Answer on Stackoverflow
Solution 6 - PhpwheelmakerView Answer on Stackoverflow
Solution 7 - PhpLupuzView Answer on Stackoverflow
Solution 8 - PhpUlfView Answer on Stackoverflow
Solution 9 - Phprekha_sriView Answer on Stackoverflow
Solution 10 - PhpCiulView Answer on Stackoverflow
Solution 11 - PhpHM2KView Answer on Stackoverflow
Solution 12 - PhpmarkcialView Answer on Stackoverflow
Solution 13 - PhpRobertPittView Answer on Stackoverflow
Solution 14 - PhpImprovView Answer on Stackoverflow
Solution 15 - PhpAnders LundView Answer on Stackoverflow
Solution 16 - PhpClément Moulin - SimpleRezoView Answer on Stackoverflow