Добро пожаловать, Гость!
Модернизация links.php - Полезный php код №80
Все категории 
 Модернизация links.php
Описание: определяет ссылки на youtube и выводит превью картинки, я использовал свою модалку, изменить на свою. Ссылки определяют заголовок ссылки и выводят его вместо надоевшего ссылка 
 Код:
 
<?
function links($msg)
{
    
$pos = strpos($msg, 'https://');
    
if ($pos === true) {
        
$msg = $msg;
        
} else {
        
$row = strpos($msg, 'http://');
        
if ($row === false) {
            
$video = strpos($msg, 'youtube');
            
if ($video === false) {
                
$res = preg_match_all('#(?:https?|ftp)://[^s,]+#i', $msg, $matches);
$title = action_getTitle($matches[0][0]);
$msg = preg_replace('(https://[w+?.w+]+[a-zA-Z0-9~!@#$%^&*()_-=+/?:;'./]+[.]*[a-zA-Z0-9/]+)', "<a href='$0' target='_blank'>$title</a>", $msg);
                
} else {
                
preg_match('%(?:youtube(?:-nocookie)?.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v<img src="/style/smiles/331.gif" alt="smile" />|youtu.be/)([^"&?/ ]{11})%i', $msg, $match);
$youtube_id = $match[1];
$msg = preg_replace('(https://[w+?.w+]+[a-zA-Z0-9~!@#$%^&*()_-=+/?:;'./]+[.]*[a-zA-Z0-9/]+)', "<div class='c-video'><a href='$0' data-fancybox='media'><img src='http://img.youtube.com/vi/$youtube_id/sddefault.jpg'></a></div>", $msg);
                
}
            
} else {
            
$res = preg_match_all('#(?:http?|ftp)://[^s,]+#i', $msg, $matches);
$title = action_getTitle($matches[0][0]);
$msg = preg_replace('(http://[w+?.w+]+[a-zA-Z0-9~!@#$%^&*()_-=+/?:;'./]+[.]*[a-zA-Z0-9/]+)', "<a href='$0' target='_blank'>$title</a>", $msg);
            
}
}
    
return $msg;
    
}
function action_getTitle($url){
    
$page = @file_get_contents($url);
$title = preg_match("|<title>([^<]+)</title>|is", $page, $matches) ? $matches[1] : '';
    
if (mb_check_encoding($title, 'Windows-1251') && !mb_check_encoding($title, 'UTF-8')){
$title = iconv("CP1251//IGNORE", "UTF-8", $title);
}
    
return $title;
    
}
?>
<?
function links($msg)
{
$pos = strpos($msg, 'https://');
if ($pos === true) {
$msg = $msg;
} else {
$row = strpos($msg, 'http://');
if ($row === false) {
$video = strpos($msg, 'youtube');
if ($video === false) {
$res = preg_match_all('#(?:https?|ftp)://[^s,]+#i', $msg, $matches);
$title = action_getTitle($matches[0][0]);
$msg = preg_replace('(https://[w+?.w+]+[a-zA-Z0-9~!@#$%^&*()_-=+/?:;'./]+[.]*[a-zA-Z0-9/]+)', "<a href='$0' target='_blank'>$title</a>", $msg);
} else {
preg_match('%(?:youtube(?:-nocookie)?.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v<img src="/style/smiles/331.gif" alt="smile" />|youtu.be/)([^"&?/ ]{11})%i', $msg, $match);
$youtube_id = $match[1];
$msg = preg_replace('(https://[w+?.w+]+[a-zA-Z0-9~!@#$%^&*()_-=+/?:;'./]+[.]*[a-zA-Z0-9/]+)', "<div class='c-video'><a href='$0' data-fancybox='media'><img src='http://img.youtube.com/vi/$youtube_id/sddefault.jpg'></a></div>", $msg);
}
} else {
$res = preg_match_all('#(?:http?|ftp)://[^s,]+#i', $msg, $matches);
$title = action_getTitle($matches[0][0]);
$msg = preg_replace('(http://[w+?.w+]+[a-zA-Z0-9~!@#$%^&*()_-=+/?:;'./]+[.]*[a-zA-Z0-9/]+)', "<a href='$0' target='_blank'>$title</a>", $msg);
}
}
return $msg;
}
function action_getTitle($url){
$page = @file_get_contents($url);
$title = preg_match("|<title>([^<]+)</title>|is", $page, $matches) ? $matches[1] : '';
if (mb_check_encoding($title, 'Windows-1251') && !mb_check_encoding($title, 'UTF-8')){
$title = iconv("CP1251//IGNORE", "UTF-8", $title);
}
return $title;
}
?>
 Добавил:  promonj (5 Апр  2020)
 promonj (5 Апр  2020)
  promonj (5 Апр  2020)
 promonj (5 Апр  2020) Просмотров: (25)
 Комментарии (0)
 Скачать: TEXTAREA
DCMS-Social.ru - движок социальной сети, вап и веб мастерская, файло-обменник и форум.
Статистика DCMS-Social.ru: Пользователей - 15118, тем на форуме - 30309, сообщений - 298969

 
 

