Advertisement
ZardoZAntony

bitrix rel="nofolow" target="_blank" для всех внешних ссылок

Sep 11th, 2019
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. /*
  2. доработанная функция из модуля
  3. https://marketplace.1c-bitrix.ru/solutions/asd.nofollow/
  4. */
  5.     if (!defined('ADMIN_SECTION') || ADMIN_SECTION !== true)
  6.         $content = preg_replace_callback('#<a([^>]+?)href\s*=\s*(["\']*)\s*(http|https|ftp)://([^"\'\s>]+)\s*\\2([^>]*?)>(.*?)</a>#is',
  7.             function ($matches) {
  8.                 $result = $matches[0];
  9.                 if (strpos($matches[0], $_SERVER["HTTP_HOST"]) === false) { //не трогаем ссылки нашим адресом
  10.                     if (strpos($matches[0], "rel=") === false && strpos($matches[0], "rel =") === false) {  //бывает разделено пробелом
  11.                         $addon = 'rel="nofollow"';
  12.  
  13.                         if (strpos($matches[0], "target=") === false && strpos($matches[0], "target =") === false) //будем открывать их в новой вкладке
  14.                             $addon .= ' target="_blank"';
  15.  
  16.                         $result = "<a$matches[1]href=$matches[2]$matches[3]://$matches[4]$matches[2]$matches[5] $addon>$matches[6]</a>";
  17.                     }
  18.                 }
  19.                 return $result;
  20.             },
  21.             $content
  22.         );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement