开发者

get text between an @ sign and a comma

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 t开发者_如何学Gohe 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.


~@([^,]+)~

[^,] means every char except ,

DEMO


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.


I think this is what you need:

/@([a-z0-9_\s]+)/i

\s is the symbol for space.


/@(.*?),/ ? makes * non-greedy, thats why it's stop after first comma. You should understand that it won't match @foo without commas


try preg_match_all

<?php
$subject = "@foo, @bar, @foo bar, hello world";
$pattern = '/@(.*),.*/';
$matches = preg_match($pattern, $subject, PREG_SET_ORDER);
print_r($matches);
?>

DEMO of REGEX PATTERN

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜