[^\s@]+@([^\s@.,]+\.)+[^\s@.,]{2,})|(?P@\w*)|(?P(?:http[s]?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*))/', function ($matches) { $match = collect($matches)->first() ?? ''; if (filter_var($match, FILTER_VALIDATE_EMAIL)) { return strlen($match) ? "$match" : $match; } elseif (isset($matches['user']) and strlen($matches['user'])) { return sprintf( '%s', $matches['user'] ); } else { $url = $match; if (\strpos($url, 'http') === false and \strpos($url, 'https') === false) { $url = sprintf('http://%s', $url); } $fsligthbox = self::isImageFile($url) ? 'data-fslightbox="gallery"' : ''; return strlen($match) ? "$match" : $match; } }, $text ); return nl2br($text); } public static function isImageFile(string $url): bool { $m = []; return \preg_match('/(\.png|\.jpg|\.jpeg|\.gif)$/', $url, $m) === 1; } }