I would like to get strings inside [square] [brackets] and also those @like @this. The following works for the [tags] but not the @users.
$query = "[tag1] [tag2] @user1 @user2 ignoreme";
// check for tags e.g. [tag1] [tag2]
preg_match_all("/\[([^\]]+)\]/", $query, $tags);
// check for users e.g. @username
preg_match_all("/\B@[^\B]+/", $query, $users);
print_r($tags);
print_r($users);
Output:
(
[0] => Array
(
[0] => [tag1]
[1] => [tag2]
)
[1] => Array
(
[0] => tag1
[1] => tag2
)
)
Array
(
[0] => Array
(
[0] => @user1 @user2 ignoreme
)
)
The tag preg_match is working but the @user one just grabs everything after it (even ignoring the second @user2).
If you look at this explanation: http://regex101.com/r/fH4lG4 you’ll find out why your regex is broken.
You’re matching everything BUT the literal
B.What you should be doing is either
[^@]+,[^@ ]+or\S+I hope this helps 🙂