ldc-supportweb/app/Lib/Utils.php
2021-05-18 21:52:44 +02:00

29 lines
1 KiB
PHP

<?php
namespace App\Lib;
class Utils
{
public static function plainToHtml(string $text)
{
$text = e($text);
// url replace and email replace
$text = preg_replace_callback(
'/([^\s@]+@([^\s@.,]+\.)+[^\s@.,]{2,})|(?: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) ? "<a href=\"mailto:$match\">$match</a>" : $match;
} else {
$url = $match;
if (\strpos($url, 'http') === false and \strpos($url, 'https') === false) {
$url = sprintf('http://%s', $url);
}
return strlen($match) ? "<a href=\"$url\" target=\"_blank\">$match</a>" : $match;
}
},
$text
);
return nl2br($text);
}
}