Reputation: 55
I found this solution on Stack Overflow for getting the first word from a sentence.
$myvalue = 'Test me more';
$arr = explode(' ',trim($myvalue));
echo $arr[0]; // will print Test
This is suitable when a space character is used to divide words. Does anyone know how to get the first word from a string if you do not know what the divider is? It can be ' ' (space), '.' (full stop), '.' (or comma).
Basically, how do you take anything that is a letter from a string up to the point where there is no letter?
E.g.:
House, rest of sentence here
would give House
House.
would also give House
House thing
would also give House
Upvotes: 1
Views: 6043
Reputation: 48073
Depending on the variability of your input string, there may be a few different approaches that will suffice. If you want maximum control and reliability, I recommend a regular expression with preg_match()
or preg_replace()
. I recommend preg_replace()
over preg_match()
because the latter generates an array, but the former directly returns the desired string.
/ #start of pattern delimiter
^ #match only from the start of the string
[a-z]* #match zero or more letters
\K #forget previously matched characters
.* #match zero or more characters until the end of the string
/ #end of pattern delimiter
i #match case-insensitively
strtok()
receives a "character mask" string as its second parameter -- this means that you can list single-byte characters in any order and it will return the string that comes before the first of any listed character.
sscanf()
is sometimes useful for parsing text, but in this case it is not as nice to play with if the string might not start with a letter.
str_word_count()
has its own definition of what a "word" is. It can consider apostrophes and hyphens as potential word characters depending on their position in the "word". You can even extend its definition by adding allowed "word" characters.
Code: (Demo)
$tests = [
'House, rest of sentence here',
'House.',
'House thing',
'',
'!House fly',
'House&Home',
];
foreach ($tests as $test) {
echo "Sample string : \t" . var_export($test, true);
echo "\nstrtok() : \t\t" . var_export(strtok($test, ',. '), true);
echo "\nsscanf() : \t\t" . var_export(sscanf($test, '%[A-Za-z]')[0] ?? '', true);
echo "\npreg_replace() : \t" . var_export(preg_replace('/^[a-z]*\K.*/i', '', $test), true);
echo "\nstr_word_count() : \t" . var_export(str_word_count($test, 1)[0] ?? '', true);
echo "\n---\n";
}
Output:
Sample string : 'House, rest of sentence here'
strtok() : 'House'
sscanf() : 'House'
preg_replace() : 'House'
str_word_count() : 'House'
---
Sample string : 'House.'
strtok() : 'House'
sscanf() : 'House'
preg_replace() : 'House'
str_word_count() : 'House'
---
Sample string : 'House thing'
strtok() : 'House'
sscanf() : 'House'
preg_replace() : 'House'
str_word_count() : 'House'
---
Sample string : ''
strtok() : false
sscanf() : ''
preg_replace() : ''
str_word_count() : ''
---
Sample string : '!House fly'
strtok() : '!House'
sscanf() : ''
preg_replace() : ''
str_word_count() : 'House'
---
Sample string : 'House&Home'
strtok() : 'House&Home'
sscanf() : 'House'
preg_replace() : 'House'
str_word_count() : 'House'
---
Upvotes: 0
Reputation: 2101
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.
Upvotes: 4
Reputation: 887
Use the preg_match()
function with a regular expression:
if (preg_match('/^\w*/', 'Your text here', $matches) > 0) {
echo $matches[0]; // $matches[0] will contain the first word of your sentence
} else {
// no match found
}
Upvotes: 0
Reputation: 9260
preg_split
is what you're looking for.
$str = "bla1 bla2,bla3";
$words = preg_split("/[\s,]+/", $str);
This snippet splits the $str by space, \t, comma, \n.
Upvotes: 2