从推文中查找URL、@回复和#主题标签

4

我正在使用PHP构建一个小型的Twitter应用,试图解析URL、@回复和#标签,并将它们转换为可点击的链接。

我找到了一个用于解析URL的类,想知道是否也可以用来解析@回复和#标签:

// http://josephscott.org/archives/2008/11/makeitlink-detecting-urls-in-text-and-making-them-links/    
class MakeItLink {
protected function _link_www( $matches ) {
    $url = $matches[2];
    $url = MakeItLink::cleanURL( $url );
    if( empty( $url ) ) {
        return $matches[0];
    }

    return "{$matches[1]}<a href='{$url}'>{$url}</a>";
}

public function cleanURL( $url ) {
    if( $url == '' ) {
        return $url;
    }

    $url = preg_replace( "|[^a-z0-9-~+_.?#=!&;,/:%@$*'()x80-xff]|i", '', $url );
    $url = str_replace( array( "%0d", "%0a" ), '', $url );
    $url = str_replace( ";//", "://", $url );

    /* If the URL doesn't appear to contain a scheme, we
     * presume it needs http:// appended (unless a relative
     * link starting with / or a php file).
     */
    if(
        strpos( $url, ":" ) === false
        && substr( $url, 0, 1 ) != "/"
        && !preg_match( "|^[a-z0-9-]+?.php|i", $url )
    ) {
        $url = "http://{$url}";
    }

    // Replace ampersans and single quotes
    $url = preg_replace( "|&([^#])(?![a-z]{2,8};)|", "&#038;$1", $url );
    $url = str_replace( "'", "&#039;", $url );

    return $url;
}

public function transform( $text ) {
    $text = " {$text}";

    $text = preg_replace_callback(
        '#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/\-=?@\[\](+]|[.,;:](?![\s<])|(?(1)\)(?![\s<])|\)))*)#is',
        array( 'MakeItLink', '_link_www' ),
        $text
    );

    $text = preg_replace( '#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $text );
    $text = trim( $text );

    return $text;
}
}

这里有问题吗?你对这段代码有困难吗? - brettkelly
是的,这个类只解析链接,我希望还能解析@回复和#标签,并将它们转换成链接。 - Tom
2个回答

20

我觉得你想做的基本上就是我下面包含的内容。 你可以在 transform 方法中添加这两个语句,在返回语句之前执行。

$text = preg_replace('#@(\w+)#', '<a href="http://twitter.com/$1">$0</a>', $text);
$text = preg_replace('/#(\w+)/', '<a href="http://twitter.com/search?q=%23$1&src=hash">$0</a>', $text);

这是你想要的吗?


我喜欢这个实现的简洁性。正确! - Stephen Nguyen
嗨@SoaperGEM,这没有考虑到标签中的特殊字符,比如说,它不能处理像#Prévoyance这样的单词。有什么解决方法吗? - Jeremy

3
Twitter最近发布了他们用于查找用户名、哈希标签、列表和URL的代码的JavaRuby(Gem)实现的开源版本。这些代码非常依赖于正则表达式。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接