-1

假设我有以下字符串:

$string = '很棒的@jason,我想@sally 也会喜欢的。'

我在用着:

$mentions = strstr($string, '@');
echo $mentions 

哪个输出

@jason I think @sally would love it too.

期望的输出

@jason @sally

我只是不知道如何在 php 中做到这一点,在 js 中我只是这样做:

hashtags = /\@\w+/g;
var matches = string.match(hashtags);
alert(matches);

php 中的 preg_match() 返回一个布尔值,所以我能得到的最接近的是 strstr()... 但在第一次匹配后返回整个字符串。

4

1 回答 1

4

您可以使用preg_match_all相同的正则表达式:

<?php

$string = 'Great @jason I think @sally would love it too.';
preg_match_all("/@\w+/", $string, $matches);

var_dump($matches);

?>

演示:http: //ideone.com/NgrRvH

于 2013-04-17T23:03:59.710 回答