Skip to content

Instantly share code, notes, and snippets.

@aktaumag
Created January 21, 2021 06:08
Show Gist options
  • Save aktaumag/4bf7f53b7b5f2117685bae71a7e32f5e to your computer and use it in GitHub Desktop.
Save aktaumag/4bf7f53b7b5f2117685bae71a7e32f5e to your computer and use it in GitHub Desktop.
Массовое удаление циклических ссылок
@aktaumag
Copy link
Author

aktaumag commented Jan 21, 2021

Часто встречаются ситуации, когда в коде PHP можно обработать весь контент перед выводом.
DataLife Engine (DLE) — это делается в конце файла /engine/modules/main.php
CMS Bitrix — это делается в файле /local/php_interface/init.php function OnEndBufferContentHandler(&$content)

// СТАРТ Михаил Носов: Удаление циклических ссылок НАЧАЛО
  $content = preg_replace_callback ( '#<a ([^>]*?)href="([^>]+?)"([^>]*?)>#is',
    function ($matches=array())
    {
        //if ( !count($matches) ) return "";
        //$matches[2] = str_replace ( 'http://'.$_SERVER['HTTP_HOST'], 'https://'.$_SERVER['HTTP_HOST'], $matches[2] );
        //$wsurlnew = '<a '.$matches[1].'href="'.$matches[2].'"'.$matches[3].'>';
        if ($matches[2] == $_SERVER['REQUEST_URI'] || $matches[2] == 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']){
            $wsitemprop = '';
            // Если текущий адрес совпадает со ссылкой
            if(strripos($matches[0], 'itemprop') !== false){
                // если в ссылке есть параметр itemprop
                $matches[0] = preg_replace('/\s+/', ' ', $matches[0]); // удаление из строки двойных пробелов и табов
                $matches[0] = str_replace(array(' =', '= '), '=', $matches[0]);
                if(strripos($matches[0], 'itemprop="url"') !== false) {
                    // если в ссылке есть параметр itemprop="url"
                    $matches[0] = str_replace('itemprop="url"', '', $matches[0]);
                    // удаляем домен, так как потом его добавим отдельно.
                    $matches[2] = str_replace ( 'https://'.$_SERVER['HTTP_HOST'], '', $matches[2] );

                    $wsitemprop = '<meta itemprop="url" content="https://'.$_SERVER['HTTP_HOST'].$matches[2].'">';
                }
            }
            $matches[0] = str_replace(array(' href='), ' data-wshref=', $matches[0]);
            $matches[0] .= $wsitemprop;
        }
        return $matches[0];
    },
    $content );
// ФИНИШ Михаил Носов: Удаление циклических ссылок КОНЕЦ

Чтобы курсор у всех таких ссылок был не как для выделения текста, то в стилях прописываем

<style>
	a[data-wshref]:not([onclick]){ cursor: default; }
	a.dropdown-toggle:not([href]):not([onclick]){cursor: pointer}
</style>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment