I need to extract all whitespace separated "words" at the end of string that either start with the @ character or that consist solely of digits.
For example, I have this string:
hello @tag1 111 world @tag2 @tag3 222 333
I need to obtain
@tag2 @tag3 222 333
I have used this code below:
preg_match_all('~\B@\S+|\b\d+\b~', $string, $match);
With this, I'll get all strings that started with '@' character and numbers, even @tag1 and 111.
I also tried the code below:
$str = 'hello @tag1 111 world @tag2 @tag3 222 333';
$exp = explode(' ', $str);
$arraysfordelete = [];
foreach ($exp as $num => $user) {
if (!preg_match('~\B@\S+|\b\d+\b~', $user, $match)) {
array_push($arraysfordelete, $num);
}
}
$array = array_slice($exp, end($arraysfordelete) + 1);
echo implode(' ', $array);
Any suggestions?