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.
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:
<?php
$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:
<?php
$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:
<?php
$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:
<?php
$sentence
=
'Welcome to GeeksforGeeks'
;
preg_match(
'/bw+b/i'
,
$sentence
,
$result
);
echo
"The first word of string is: "
.
$result
[0];
?>
Output:
The first word of string is: Welcome
Hi Dev,
This tutorial is focused on php get first word from string example. I’m going to show you about how to get first word from string in php?. I would like to share with you get first word from string in php example. you’ll learn how to use function to get first word from string in php? .
There are tow example to get First word from strings in PHP. in this example, we will use to strtok() and preg_match() function to remove First word. so Let’s both the following example with output.
Example 1: Get First Word from String
index.php
<?php
$str = "Welcome to Nicesnippets.com";
//Get first word of a string
$result = strtok($str, " ");
echo $result;
?>
Output:
Welcome
Example 2: Get First Word from String
index.php
<?php
$str = 'Hello all Student';
//Get first word of a string
preg_match('/bw+b/i', $str, $result);
echo $result[0];
?>
Output:
Hello
I hope it could help you…
#PHP
✌️ Like this article? Follow me on Twitter and Facebook. You can also subscribe to RSS Feed.
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.
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.
Alternative solutions
$myvalue = 'Test me more';
echo strstr($myvalue, ' ', true);
// will print Test
echo current(explode(' ',$myvalue));
// will print Test
$myvalue = 'Test me more';
$arr = explode(' ',trim($myvalue));
echo $arr[0]; // will print Test
$string = ' Test me more '; preg_match('/bw+b/i', $string, $result); echo $result; // will print Test /* 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); echo $result; // will print Test
Ref: http://stackoverflow.com/questions/2476789/how-to-get-the-first-word-of-a-sentence-in-php