以开头的PHP/jQuery超链接文本@

以开头的PHP/jQuery超链接文本@,php,Php,有人能建议最好的方法吗。我正在从MySQL中检索相当于推特内容的内容,并将其回传出来 有时,内容包含一个@username,作为文本的一部分。我需要一种方法将@username部分超链接到/username 例如,如果PHP是: $tweetcontent = 'Hey @alex how are you?'; echo $tweetcontent; 内容应为: <body>Hey <a href="/alex">@alex</a> how are you?

有人能建议最好的方法吗。我正在从MySQL中检索相当于推特内容的内容,并将其回传出来

有时,内容包含一个
@username
,作为文本的一部分。我需要一种方法将
@username
部分超链接到
/username

例如,如果PHP是:

$tweetcontent = 'Hey @alex how are you?';
echo $tweetcontent;
内容应为:

<body>Hey <a href="/alex">@alex</a> how are you?</body>
嘿,你好吗?

谢谢

希望这里的linkify功能能对您有所帮助

/**
* Turn all URLs in clickable links.
*
* @param string $value
* @param array $protocols http/https, ftp, mail, twitter
* @param array $attributes
* @param string $mode normal or all
* @return string
*/
    function linkify($value, $protocols = array('http', 'mail'), array $attributes = array(), $mode = 'normal')
    {
        // Link attributes
        $attr = '';
        foreach ($attributes as $key => $val) {
            $attr = ' ' . $key . '="' . htmlentities($val) . '"';
        }

        $links = array();

        // Extract existing links and tags
        $value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) { return '<' . array_push($links, $match[1]) . '>'; }, $value);

        // Extract text links for each protocol
        foreach ((array)$protocols as $protocol) {
            switch ($protocol) {
                case 'http':
                case 'https': $value = preg_replace_callback($mode != 'all' ? '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i' : '~([^\s<]+\.[^\s<]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { if ($match[1]) $protocol = $match[1]; $link = $match[2] ?: $match[3]; return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $link . '">' . $link . '</a>') . '>'; }, $value); break;
                case 'mail': $value = preg_replace_callback('~([^\s<]+?@[^\s<]+?\.[^\s<]+)(?<![\.,:])~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="mailto:' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break;
                case 'twitter': $value = preg_replace_callback('~(?<!\w)[@#](\w++)~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="https://twitter.com/' . ($match[0][0] == '@' ? '' : 'search/%23') . $match[1] . '">' . $match[0] . '</a>') . '>'; }, $value); break;
                default: $value = preg_replace_callback($mode != 'all' ? '~' . preg_quote($protocol, '~') . '://([^\s<]+?)(?<![\.,:])~i' : '~([^\s<]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break;
            }
        }

        // Insert all link
        return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value);
    }
/**
*在可点击链接中打开所有URL。
*
*@param字符串$value
*@param array$协议http/https、ftp、邮件、twitter
*@param数组$attributes
*@param string$模式正常或全部
*@返回字符串
*/
函数linkify($value,$protocols=array('http','mail'),array$attributes=array(),$mode='normal')
{
//链接属性
$attr='';
foreach($key=>$val的属性){
$attr='.$key.'='.htmlentities($val)。';
}
$links=array();
//提取现有链接和标记
$value=preg_replace_回调(“~(”).>”;},$value);打破
case'mail':$value=preg_replace_回调(“~([^\s/”,函数($match)use(&$links){return$links[$match[1]-1];},$value);
}

参考

我会用一个简单的正则表达式,Sasikumar的答案有点过分,我认为Jeff的答案不适合你的情况

$string = 'Hey @alex, how are you? @user_123';

//find all string that start with @ and is followed by 
//any amount of lowercase and uppercase letters 
//as well as and underscore "_" and numbers 0-9
preg_match_all('/@[a-zA-Z0-9_]+/', $string, $matches);

//loop through all matches
foreach($matches[0] as $match)
{
    //get username without "@"
    $username = substr($match, 1);

    //create a link (note: $match is username with "@" sign
    $link = '<a href="/' . $username . '">' . $match . '</a>';

    //replace all matches with links
    $string = str_replace($match, $link, $string);   
}

//output for testing
echo $string;
$string='Hey@alex,你好吗?@user_123';
//查找所有以@开头并后跟的字符串
//大小写字母的任意数量
//以及下划线“u”和数字0-9
preg_match_all('/@[a-zA-Z0-9_]+/',$string,$matches);
//循环遍历所有匹配项
foreach($matches[0]为$match)
{
//获取不带“@”的用户名
$username=substr($match,1);
//创建链接(注意:$match是带有“@”符号的用户名
$link='';
//用链接替换所有匹配项
$string=str_replace($match,$link,$string);
}
//测试输出
echo$字符串;
试试这个

<?php
$str = "hi @google and @yahoo";
$regex = '/(@(\\w+))/i';// a simple regex additional validaton needs to be done by you

$sub = "<a href=\"$2\">$1</a>";
$result = preg_replace($regex , $sub , $str);

echo $result;
?>


?你想要PHP还是JavaScript解决方案?到目前为止你尝试了什么?@lvaroGonzález我在寻求建议,以便找到最好的方法。这就是为什么我同时包含了这两个标签。我为没有回复你的评论而道歉。很公平。我正在删除我的评论,这本可以更好。嗨@Sasikumar抱歉,如果这是显而易见的,但是h我如何通过它运行和输出一个变量。算出它。只需在函数之后执行
echo linkify(@username”);
。注意,对于将来使用该函数的人,您必须将
'twitter'
添加到
$protocols=array('http','mail'))
part.虽然@Sasikumar为您提供了一个完整的解决方案,但您只能自己使用twitter case,比如:echo preg_replace_callback(“~”(?@Ben,如果您像linkify一样调用linkify,那么该数组只是默认值(“@adam看看这个”,数组(“http”,“twitter”));它将在运行时更改数组,使其仅包含http和twitterlinks@Jeff如果这个答案对你有效,请接受它,因为它可能会帮助其他人。我没有问这个问题,它比我的答案好,因为它允许多个用户,所以我说它很有效。