1

嘿伙计们,我正在使用以下内容:

$pos1 = strpos($currentStatus, '#');
$pos2 = strpos($currentStatus, '#', $pos1 + strlen('#'));

如果它发现第一个标签然后寻找第二个标签,那么它的作用是获取第二个标签......所以它抓住了它,我将它存储到变量中并打印出来......问题?当我打印出来时,我得到了字符串的其余部分,例如:

$code = "Hi lets have #funfgs and than more #funny yup yup";
$pos1 = strpos($code , '#');
$pos2 = strpos($code , '#', $pos1 + strlen('#'));

echo substr($code , $pos2);

结果:#funny yup yup

所以我想要带有连接词的主题标签,其余的被扔掉......我该怎么做呢?

大卫

编辑:

我想要的是:#funny

4

3 回答 3

3

您担心的答案是使用preg_matchfunction

所以,为了你的使用

$code = "Hi lets have #funfgs and than more #funny yup yup";
$pos1 = preg_match( "/.*#(\S+)/", $code , $match );
print_r( $match[1] );

您也可以#为您的比赛添加 。以下是你的做法:

$pos1 = preg_match( "/.*(#\S+)/", $code , $match );
echo $match[1];
于 2013-03-22T05:18:35.203 回答
2

尝试这个 :

$code = "Hi lets have #funfgs and than more #funny yup yup";

preg_match_all('/#(?P<hash>\w+)/',$code,$match);

echo "<pre>";
print_r($match['hash']);

在这里,#您可以从数组中选择任何单词后获得所有单词$match['hash']

对于您的问题中提到的情况,请使用echo $match['hash'][1];

于 2013-03-22T05:24:42.540 回答
1

您可以使用爆炸功能:

$code = "Hi lets have #funfgs and than more #funny yup yup";
$pos1 = strpos($code , '#');
$pos2 = strpos($code , '#', $pos1 + strlen('#'));

$hashtag = explode(' ', substr($code , $pos2));

echo $hashtag[0];
于 2013-03-22T05:22:11.963 回答