private static linkToText ( string $text ) : string | ||
$text | string | Text with HTML fragments |
return | string |
private static function linkToText(string $text) : string
{
return preg_replace_callback('~<a\\s+(?:[^>]+\\s+)*href\\s*=\\s*"([^"]+)"(?:\\s+[^>]*)?>(.+?)</a>~is', function ($matches) {
$url = trim($matches[1]);
$content = $matches[2];
$clearContent = trim(strip_tags($content));
// Some urls have no real meaning
if (empty($url) || '#' === $url[0] || '/?' === substr($url, 0, 2)) {
return $content;
}
// Invalid url gets ignored
if (!Input\Validator\IsUrl::validate($url)) {
return $content;
}
// If the link text and target are the same, use only one of them
if ($url === $clearContent) {
return '[textlink]' . $content . '[/textlink]';
} else {
return $content . ' [textlink]' . $url . '[/textlink]';
}
}, $text);
}