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?
This question is related to
php
string
extract
text-segmentation
$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
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.
public function getStringFirstAlphabet($string){
$data='';
$string=explode(' ', $string);
$i=0;
foreach ($string as $key => $value) {
$data.=$value[$i];
}
return $data;
}
Similar to accepted answer with one less step:
$my_value = 'Test me more';
$first_word = explode(' ',trim($my_value))[0];
//$first_word == 'Test'
$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 is PHP str_word_count() multibyte safe?
All of the answers here are using an approach that the processor needs to search all of the string even if the first word is found! For big strings, this is not recommended. This approach is optimal:
function getFirstWord($string) {
$result = "";
foreach($string as $char) {
if($char == " " && strlen($result)) {
break;
}
$result .= $char;
}
return $result;
}
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.
You could do
echo current(explode(' ',$myvalue));
Since you can not check with strok for upper or lowercase this works perfectly for checking the first word.
if (strtolower(strtok($text, " ")) == strtolower($firstword)){ .. }
<?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.
$input = "Test me more"; echo preg_replace("/\s.*$/","",$input); // "Test"
Using split function also you can get the first word from string.
<?php
$myvalue ="Test me more";
$result=split(" ",$myvalue);
echo $result[0];
?>
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.
Even though it is little late, but PHP has one better solution for this:
$words=str_word_count($myvalue, 1);
echo $words[0];
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.
$str='<?php $myvalue = Test me more; ?>';
$s = preg_split("/= *(.[^ ]*?) /", $str,-1,PREG_SPLIT_DELIM_CAPTURE);
print $s[1];
You Can do it using PHP string function substr without conveting string into array.
$string = 'some text here';
$stringLength= strlen($string);
echo ucfirst(substr($string,-$stringLength-1, 1));
//output S
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
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.
Function that will tokenize string into two parts, first word and remaining string.
Return Value: It will have first
and remaining
key in $return
array respectively.
first check strpos( $title," ") !== false
is mandatory in case when string has only one word and no space in it.
function getStringFirstWord( $title ){
$return = [];
if( strpos( $title," ") !== false ) {
$firstWord = strstr($title," ",true);
$remainingTitle = substr(strstr($title," "), 1);
if( !empty( $firstWord ) ) {
$return['first'] = $firstWord;
}
if( !empty( $remainingTitle ) ) {
$return['remaining'] = $remainingTitle;
}
}
else {
$return['first'] = $title;
}
return $return;
}
Source: Stackoverflow.com