爆炸多个Hashtags

时间:2016-07-11 14:28:39

标签: php hashtag

我目前正在为本地组开发一个轻量级的内部消息传递脚本,该脚本利用自定义的BBCode样式解析器将论坛样式标记(如[b])转换为HTML等效标记。

我还包括一个类似于主题标签的Twitter功能,因此人们可以在消息系统中标记和跟踪讨论,这很有效,直到用户在文本中输入多个主题标签。

通常,用户会将其文本输入到提交表单中。

Lorum Ipsum  dolor sit amet, #consectetur adipiscing elit.

使用SQL,讨论跟踪器加载最后20条消息并循环遍历它们,每次调用TrackDiscussion($ i);功能。 $ i参数是文本/提交的位置,用于检查主题标签。

function TrackDiscussion($i,$str=1) {
    $keywords="";
    preg_match_all('/#(\w+)/',$i,$matches);
    $i = 0;
    if($str){
        foreach($matches[1] as $match) {
            $count=count($matches[1]);
            $keywords .= "$match";
            $i++;
            if ($count>$i) $keywords .= ", ";
       }
    }
    else{
        foreach($matches[1] as $match) {
            $keyword[]=$match;
        }
        $keywords=$keyword;
    }
    return $keywords;
}

一切正常,直到它在文本中找到多个#标签,而不是输出:

<a href="http://example.com/tag/hashtag1">#hashtag1</a>, <a href="http://example.com/tag/hashtag2">#hashtag2</a>

输出:

<a href="http://example.com/tag/hashtag1,hashtag2">#hashtag1, #hashtag2</a>

我为凌乱的代码道歉,但我猜想我是否应该将关键字分开,最有可能是爆炸功能?

如果有人能给我一个关于我会把它放在哪里的指针,我会非常感激,我一次又一次地去看它,我似乎无法找到我的错误。

由于

2 个答案:

答案 0 :(得分:2)

这样的东西?

function TrackDiscussion( $input ) {
    return preg_replace( "~\#(\w+)~i", '<a href="http://example.com/tag/$1">#$1</a>', $input );
}

echo TrackDiscussion( 'Lorum #Ipsum  dolor sit amet, #consectetur adipiscing #elit.' );

或者只包含主题标签链接?

function TrackDiscussion2( $input ) {
    preg_match_all( "~\#(\w+)~i", $input, $matches );
    $return = array();
    foreach( $matches[1] as $match ) {
        $return[] .= '<a href="http://example.com/tag/'.$match.'">#'.$match.'</a>';
    }
    return implode( ', ', $return );
}

echo TrackDiscussion2( 'Lorum #Ipsum  dolor sit amet, #consectetur adipiscing #elit.' );

答案 1 :(得分:1)

我已将逻辑分解为单独的函数,以使事情变得尽可能简单。 这就是你要追求的吗?

$last20Messages = array(
    'This is just some random stuff. #random #stuff',
    'And some more. #some #more',
);
echo getTagLinksForMessages($last20Messages);

输出:

<a href='/tag/random'>#random</a>
<a href='/tag/stuff'>#stuff</a>
<a href='/tag/some'>#some</a>
<a href='/tag/more'>#more</a>

功能定义:

/**
 * @param string $message
 * @return array
 */
function getTagsFromMessage($message)
{
    preg_match_all('/#(\w+)/', $message, $matches);
    return $matches[1];
}

/**
 * @param array|string[] $messages
 * @return array
 */
function getTagsFromMessages(array $messages)
{
    $tags = [];
    foreach ($messages as $message) {
        $messageTags = getTagsFromMessage($message);
        $tags = array_merge($tags, $messageTags);
    }
    $tags = array_unique($tags);

    // You can then sort the tags here. E.g. alphabetical order.
    return $tags;
}

/**
 * @param array $tags
 * @return string
 */
function getTagLinksString(array $tags)
{
    $result = '';
    foreach ($tags as $tag) {
        $result .= "<a href='/tag/{$tag}'>#{$tag}</a>";
    }
    return $result;
}

/**
 * @param array $messages
 * @return string
 */
function getTagLinksForMessages(array $messages)
{
    $tags = getTagsFromMessages($messages);
    return getTagLinksString($tags);
}
相关问题