Advertisement
Guest User

Text linkek átalakítása kattinthatóvá

a guest
May 9th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. function linkify($value, $protocols = array('http', 'mail'), array $attributes = array())
  2. {
  3. // Link attributes
  4. $attr = '';
  5. foreach ($attributes as $key => $val) {
  6. $attr = ' ' . $key . '="' . htmlentities($val) . '"';
  7. }
  8.  
  9. $links = array();
  10.  
  11. // Extract existing links and tags
  12. $value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) { return '<' . array_push($links, $match[1]) . '>'; }, $value);
  13.  
  14. // Extract text links for each protocol
  15. foreach ((array)$protocols as $protocol) {
  16. switch ($protocol) {
  17. case 'http':
  18. case 'https': $value = preg_replace_callback('~(?:(https?)://([^\s<]+)|(www\.[^\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;
  19. 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;
  20. 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;
  21. default: $value = preg_replace_callback('~' . preg_quote($protocol, '~') . '://([^\s<]+?)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { return '<' . array_push($links, "<a $attr href=\"$protocol://{$match[1]}\">{$match[1]}</a>") . '>'; }, $value); break;
  22. }
  23. }
  24.  
  25. // Insert all link
  26. return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value);
  27. }
  28.  
  29. function link_it_456($content)
  30. {
  31. $content= linkify($content);
  32.  
  33.  
  34. return $content;
  35. }
  36.  
  37. add_filter( 'the_content', 'link_it_456' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement