So I am doing a list sort of like twitter where I want members to be able to tweat at each other.
What I want to do is compose a regular expression that will extract all data between the @sign and a comma.
For instance,
@foo, @bar, @foo bar, hello world
Currently I have the following expression.
/@([a-z0-9_]+)/i
However, that will stop at the space so instead of registering "@foo bar" as on member it will recognize it at just @foo and ignore the bar portion.
Can somebody help me alter that query so that usernames are allowed to have spaces.
I think this is what you need:
/@([a-z0-9_\s]+)/i
\s
is the symbol for space.
try preg_match_all
<?php
$subject = "@foo, @bar, @foo bar, hello world";
$pattern = '/@(.*),.*/';
$matches = preg_match($pattern, $subject, PREG_SET_ORDER);
print_r($matches);
?>
If you truly seek to match all the data between the @ sign and the comma, you can use
/@(.+),/i
But what I think you need is...
/@([\w +]+),/i
...which matches all word characters (letters, numbers and underscores!) and spaces between the @ and the comma signs. View the demo.
/@(.*?),/
?
makes *
non-greedy, thats why it's stop after first comma. You should understand that it won't match @foo
without commas