I wanna print the first two words of a sentence,
$txt = "Laravel PHP Node JS";
and expecting the output Laravel PHP
used below snippet is there any other way to simplify it?
echo (implode(array_slice(preg_split("/\s+/", $txt), 0, 2),' '));
I wanna print the first two words of a sentence,
$txt = "Laravel PHP Node JS";
and expecting the output Laravel PHP
used below snippet is there any other way to simplify it?
echo (implode(array_slice(preg_split("/\s+/", $txt), 0, 2),' '));
Copyright © 2021 Jogjafile Inc.
If with simpler you mean a single function call, you might for example match the first 2 "words" by matching 2 times one or more non whitespace characters with
\S+with preg_match and check if there is a match.Output