การแปลง ข้อความ www ftp http หรือ email ให้เป็นลิงค์

ด้วยการใช้ฟังก์ชั่น preg_replace

<?
  //link (http, www, ftp, email)
  $patterns = array ('#(^|[^\"\']\s)(www|WWW)\.([^\s<>\.]+)\.([^\"\'\s <>]+)#sm',
    '#(^|[^\"\'=]{1})(http|HTTP|ftp)(s|S)?://([^\s<>\.]+)\.([^\"\'\s\)<>]+)#sm',
    '/([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)/i'
    );
  $replace = array ('<a href="http://.." target="_blank">..</a>',
    '<a href="://." target="_blank">://.</a>',
    '<a href=\"mailto:@\">@</a>'
    );
  $temp = preg_replace($patterns, $replace, $temp);
?>

ผู้เขียน goragod โพสต์เมื่อ 02 เม.ย. 2551 เปิดดู 4,485 ป้ายกำกับ PHP
^