-
-
Save shahednur/a64edb86bcbd40fc2d3eea129cbb3d47 to your computer and use it in GitHub Desktop.
PHP function to turn all URLs in clickable links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 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 | |
*/ | |
public function linkify($value, $protocols = array('http', 'mail'), array $attributes = array()) | |
{ | |
// Link attributes | |
$attr = ''; | |
foreach ($attributes as $key => $val) { | |
$attr = ' ' . $key . '="' . htmlentities($val) . '"'; | |
//for multiple attributes use the following code | |
$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('~(?:(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; | |
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('~' . 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; | |
} | |
} | |
// Insert all link | |
return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Function used to turn tags into links | |
*/ | |
function tags($input,$type,$sep=', ',$class="") | |
{ | |
//Exploding using comma | |
$tags = explode(',',$input); | |
$count = 1; | |
$total = count($tags); | |
$new_tags = ''; | |
foreach($tags as $tag) | |
{ | |
$params = array('name'=>'tag','tag'=>trim($tag),'type'=>$type); | |
$new_tags .= '<a href="'.cblink($params).'" class="'.$class.'">'.$tag.'</a>'; | |
if($count<$total) | |
$new_tags .= $sep; | |
$count++; | |
} | |
return $new_tags; | |
} | |
/** | |
* Function used to assign link | |
* @param : { array } { $params } { an array of parameters } | |
* @return : { stirng } { buils link } | |
*/ | |
function cblink($params) { | |
$name = getArrayValue($params, 'name'); | |
$ref = getArrayValue($params, 'ref'); | |
if($name=='tag') { | |
return BASEURL.'/search_result.php?query='.urlencode($params['tag']).'&type='.$params['type']; | |
} | |
$param_link = ""; | |
if(!empty($params['extra_params'])) { | |
preg_match('/\?/',$link,$matches); | |
if(!empty($matches[0])) { | |
$param_link = '&'.$params['extra_params']; | |
} else { | |
$param_link = '?'.$params['extra_params']; | |
} | |
} | |
if(isset($params['assign'])) { | |
assign($params['assign'],$link.$param_link); | |
} else { | |
return $link.$param_link; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Allow display images and youtube video: | |
CSS class for links .htmllink and images .htmlimg | |
public function linkify($showimg = 1, $value, $protocols = array('http', 'mail', 'https'), array $attributes = array('target' => '_blank')) | |
{ | |
// 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('~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i', | |
function ($match) use ($protocol, &$links, $attr, $showimg) { | |
if ($match[1]){ | |
$protocol = $match[1]; $link = $match[2] ?: $match[3]; | |
// Youtube | |
if($showimg == 1){ | |
if(strpos($link, 'youtube.com')>0 || strpos($link, 'youtu.be')>0){ | |
$link = '<iframe width="100%" height="315" src="https://www.youtube.com/embed/'.end(explode('=', $link)).'?rel=0&showinfo=0&color=orange&iv_load_policy=3" frameborder="0" allowfullscreen></iframe>'; | |
return '<' . array_push($links, $link) . '></a>'; | |
} | |
if(strpos($link, '.png')>0 || strpos($link, '.jpg')>0 || strpos($link, '.jpeg')>0 || strpos($link, '.gif')>0 || strpos($link, '.bmp')>0){ | |
return '<' . array_push($links, "<a $attr href=\"$protocol://$link\" class=\"htmllink\"><img src=\"$protocol://$link\" class=\"htmlimg\">") . '></a>'; | |
} | |
} | |
return '<' . array_push($links, "<a $attr href=\"$protocol://$link\" class=\"htmllink\">$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]}\" class=\"htmllink\">{$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] . "\" class=\"htmllink\">{$match[0]}</a>") . '>'; }, $value); break; | |
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]}\" class=\"htmllink\">{$match[1]}</a>") . '>'; }, $value); break; | |
} | |
} | |
// Insert all link | |
return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment