แปลง ข้อความ http ftp ให้เป็น link

ใช้งาน preg_replace แปลงข้อความ http และ ftp ให้เป็นลิงค์ อีกวิธีหนึ่ง

สำหรับลิงค์ที่มี www

<?
  $txt = "http://www.localhost/index.php?id=123 ";
  $txt = preg_replace('=([^\s]*)(www.)([^\s]*)=' , '<a href="http://" target=\'_new\'></a>', $txt);
  echo $txt;
?>

สำหรับลิงค์ www หรือ ftp ที่มี http นำหน้า และเป็น ตัวแรกของบรรทัดเท่านั้น เช่น http://www.xxx.com

$temp = preg_replace("#(^[ ])([\w]+?://[^ \" \t<]*)#is" , "<a href=\"\" target=\"_blank\"></a>", $temp);

สำหรับลิงค์ www หรือ ftp ที่ไม่มี http และเป็น ตัวแรกของบรรทัดเท่านั้น เช่น www.xxx.com

$txt = preg_replace("#(^[ ])((wwwftp)\.[^ \"\t <]*)#is" , "<a href=\"http://\" target=\"_blank\"></a>", $txt);
ผู้เขียน goragod โพสต์เมื่อ 31 มี.ค. 2551 เปิดดู 5,237 ป้ายกำกับ PHP
^