Get first word in string 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.

';

$before = microtime(true);
for ($i=0 ; $i<1000000 ; $i++) {
    strstr($strTest, ' ', true);
}
$after = microtime(true);
echo 'strstr: '.($after-$before)/$i . ' seconds
'; $before = microtime(true); for ($i=0 ; $i<1000000 ; $i++) { current(explode(' ',$strTest)); } $after = microtime(true); echo 'explode/ current: '.($after-$before)/$i . ' seconds
'; $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
'; $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
'; $before = microtime(true); for ($i=0 ; $i<1000000 ; $i++) { strtok($value, ' '); } $after = microtime(true); echo 'strtok: '.($after-$before)/$i . ' seconds
'; ?>

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.

The first word of a sentence can be obtained with the help of various inbuilt functions like strtok(), strstr(), explode() and some other methods like using regular expressions. Some examples to get the first word of a sentence are listed below:

Method 1: Using strtok() Function: This function is used to tokenize a string into smaller parts on the basis of given delimiters. It takes input String as an argument along with delimiters (as second argument).

Syntax:

string strtok( $string, $delimiters )

Program:

$sentence = 'Welcome to GeeksforGeeks';

echo "The first word of string is: "

        . strtok($sentence, " ");

?> 

Output:

The first word of string is: Welcome

Method 2: Using trim() and explode() Function:

  • trim() Function: The trim() function is used to remove the whitespaces and also the predefined characters from both sides of a string that is left and right.

    Syntax:

    trim( $string, $charlist )
  • explode() Function: The explode() function is used to split a string in different strings. The explode() function splits a string based on the string delimiter, i.e. it splits the string wherever the delimiter character occurs. This function returns an array containing the strings formed by splitting the original string.

    Syntax:

    array explode( separator, OriginalString, NoOfElements )

Program:

$sentence = 'Welcome to GeeksforGeeks';

$arr = explode(' ', trim($sentence));

echo "The first word of string is: " . $arr[0];

?> 

Output:

The first word of string is: Welcome

Method 3: Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string. This function is case-sensitive.

Syntax:

strstr( $string, $search, $before )

Program:

$sentence = 'Welcome to GeeksforGeeks';

echo "The first word of string is: " 

    . strstr($sentence, ' ', true);

?> 

Output:

The first word of string is: Welcome

Method 4: Using preg_match() Function: This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.

Syntax:

int preg_match( $pattern, $input, $matches, $flags, $offset )

Program:

$sentence = 'Welcome to GeeksforGeeks';

preg_match('/\b\w+\b/i', $sentence, $result); 

echo "The first word of string is: ".$result[0];

?> 

Output:

The first word of string is: Welcome


How to get first word in PHP string?

echo "The first word of string is: " . $arr [0]; ?> Method 3: Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string.

What is Strstr PHP?

Definition and Usage. The strstr() function searches for the first occurrence of a string inside another string. Note: This function is binary-safe. Note: This function is case-sensitive. For a case-insensitive search, use stristr() function.

How do I remove a word from a string in PHP?

Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.