tôi viết chức năng này. Nó thay thế tất cả các liên kết trong một chuỗi. Liên kết có thể trong các định dạng sau:
Đối số thứ hai là mục tiêu cho liên kết (' _blank ',' _top '... có thể được đặt thành false). Hy vọng nó sẽ giúp ...
public static function makeLinks($str, $target='_blank')
{
if ($target)
{
$target = ' target="'.$target.'"';
}
else
{
$target = '';
}
// find and replace link
$str = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="$1" '.$target.'>$1</a>', $str);
// add "http://" if not set
$str = preg_replace('/<a\s[^>]*href\s*=\s*"((?!https?:\/\/)[^"]*)"[^>]*>/i', '<a href="http://$1" '.$target.'>', $str);
return $str;
}
Nguồn
2014-10-28 09:53:11
trùng lặp: Xem # 1.038.284 - http://stackoverflow.com/questions/1038284/php-parse-links-emails – Sampson