嘿,伙计们,我正在使用以下内容:
$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
您担心的答案是使用preg_match
函数
所以,对于你的使用
$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];
试试这个:
$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];
你可以使用爆炸函数:
$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];