我使用这个:正则表达式没有空格提取哈希标签
$t = "#hashtag #goodhash_tag united states #l33t this";
$queryVariable = "";
if(preg_match_all('/(^|\s)(#\w+)/', $t, $arrHashTags) > 0){
array_filter($arrHashTags);
array_unique($arrHashTags);
$count = count($arrHashTags[2]);
if($count > 1){
$counter = 1;
foreach ($arrHashTags[2] as $strHashTag) {
if (preg_match('/#\d*[a-z_]+/i', $strHashTag)) {
if($counter == $count){
$queryVariable .= $strHashTag;
} else{
$queryVariable .= $strHashTag." and ";
}
$newTest = str_replace($arrHashTags[2],"", $t);
}
$counter = $counter + 1;
}
}
}
echo $queryVariable."<br>"; // this is list of tags
echo $newTest; // this is the remaining text
基于上述$t
的输出是:
#hashtag and #goodhash_tag and #l33t
united states this
第一个问题:
如果$t = '#hashtag#goodhash_tag united states #l33t this';
即不两个标记之间的空间,输出变为:
#hashtag and #l33t
#goodhash_tag united states this
问题二:
如果$t = '#hashtag #goodhash_tag united states #l33t this #123';
即具有无效标签#123
它在某种程度上扰乱了我的标签列表中$queryVariable
提取等的输出变得若有人在这两个
#hashtag and #goodhash_tag and #l33t and // note the extra 'and'
united states this
请帮助?
我会很感激,如果有人可以帮助莫名其妙除了upvoting问题:) – coder101 2013-03-21 06:27:24
一个小的建议是使用'(#A-Z _] \ w +)'代替。 =) – hjpotter92 2013-03-21 06:29:52
@DreamEater你的意思是说,在'preg_match'被使用的那一行,你想让我替换整个模式还是其中的一部分? – coder101 2013-03-21 06:31:41