从推文中查找网址,@ replies和#hashtags

时间:2009-07-21 12:53:08

标签: php regex text twitter

我正在用PHP构建一个小小的Twitter,我正在尝试解析URL,@ replies和#hashtags并将它们变成可点击的链接。

我找到了一个用于解析URL的类,我想知道这是否也可用于解析@replies和#hashtags:

// 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;
}
}

2 个答案:

答案 0 :(得分:20)

我认为你要做的事情基本上就是我在下面所包含的内容。您可以在transform方法中添加这两个语句,就在return语句之前。

$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);

这就是你要找的东西吗?

答案 1 :(得分:3)

Twitter recently released开源javarubygem)用于查找用户名,哈希标记,列表和网址的代码实现。< / p>

这是非常规律的表达方式。