2016-09-23 83 views
2
開頭

有人可以建議最好的方法來解決這個問題。我正在從MySQL獲取Tweets內容的等價物並將其回顯出來。PHP/jQuery超鏈接文本以@

有時,內容包含@username作爲文本的一部分。我需要一種方法來將@username部分超鏈接到/username

E.g.如果PHP是:

$tweetcontent = 'Hey @alex how are you?'; 
echo $tweetcontent; 

內容應該成爲:

<body>Hey <a href="/alex">@alex</a> how are you?</body> 

謝謝!

+0

['str_replace'](http://php.net/manual/en/function.str-replace.php)? –

+0

你想要一個PHP或JavaScript解決方案嗎?你試過什麼了? –

+0

@ÁlvaroGonzález我在尋求建議,以便找到最好的方法。這就是爲什麼我包含這兩個標籤。我很抱歉不回覆您的評論。 – Ben

回答

2

希望從這裏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<][email protected][^\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); 
    } 

借鑑https://gist.github.com/jasny/2000705

+0

嗨@Sasikumar抱歉,如果它很明顯,但我如何運行並通過它輸出一個變量。 – Ben

+0

想通了。只要在函數後面執行'echo linkify(「@ username」);''。請注意,雖然對於將來使用此方法的任何人,您必須在'$ protocols = array('http','mail')'部分添加''twitter''。 – Ben

+0

儘管@Sasikumar給了你一個完整的解決方案,但你可以只使用自己的twitter案例,如: echo preg_replace_callback('〜(?<!\ w)[@#](\ w ++)〜',function($ match )使用(&$ links,$ attr){return'<'。array_push($ links,''。$ match [0]。'')。'>';},$ userName) ; –

1

我會用一個簡單的正則表達式做到這一點,Sasikumar的答案是有點大材小用,而且我認爲傑夫的答案對你的情況並不理想。

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

嘗試這個

<?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; 
?> 
+0

這個作品很好 – Jeff

+0

@Jeff如果這個答案適合你,請接受它,因爲它可能會幫助其他人 –

+0

我沒有問這個問題,它比我好回答是因爲它允許多個用戶,所以我說它很好用。 – Jeff