我使用像twitter和instagram这样的提及系统,你只需要放置@johndoe
我试图做的是能够删除“@”和这些字符之间的名称?
,,
,]
,:
,{{1 }}
作为一个例子,我的字符串:
(space)
如何获得hey @johnDoe check out this event, be sure to bring @janeDoe:,@johnnyappleSeed?, @johnCitizen] , and @fredNerk
,janeDoe
,johnnyappleSeed
,johnCitizen
的数组,而不包含字符fredNerk
,?
,{{ 1}},,
附在他们身上。
我知道我必须使用]
的变体,但我对它没有很强的理解。
答案 0 :(得分:1)
这就是你要求的:/\@(.*?)\s/
这就是你真正想要的:/\b\@(.*?)\b/
将其中一个放入preg_match_all()
并评估结果数组。
答案 1 :(得分:1)
preg_match_all("/\@(.*?)\s/", $string, $result_array);
答案 2 :(得分:0)
$check_hash = preg_match_all ("/@[a-zA-Z0-9]*/g", $string_to_match_against, $matches);
然后你可以做类似
foreach ($matches as $images){
echo $images."<br />";
}
更新:刚刚意识到您要删除无效字符。更新后的脚本应该这样做。
答案 3 :(得分:0)
怎么样:
$str = 'hey @johnDoe check out this event, be sure to bring @janeDoe:,@johnnyappleSeed?, @johnCitizen] , and @fredNerk';
preg_match_all('/@(.*?)(?:[?, \]: ]|$)/', $str, $m);
print_r($m);
<强>输出:强>
Array
(
[0] => Array
(
[0] => @johnDoe
[1] => @janeDoe:
[2] => @johnnyappleSeed?
[3] => @johnCitizen]
[4] => @fredNerk
)
[1] => Array
(
[0] => johnDoe
[1] => janeDoe
[2] => johnnyappleSeed
[3] => johnCitizen
[4] => fredNerk
)
)
<强>解释强>
The regular expression:
(?-imsx:@(.*?)(?:[?, \]: ]|$))
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
@ '@'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[?, \]: ] any character of: '?', ',', ' ', '\]',
':', ' '
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------