ฟังก์ชั่น Replace Links ด้วย Regular expressions ของ PHP

บันทึกโดย TaTump วันที่ 12 January 2008 | 432 views

ลักษณะการทำงานก็คือจะสร้างไฮเปอร์ลิงค์ให้โดยอัตโนมัติ
code

function replaceLinks($text) {

// convert support@pogoda.in into

// <a href="mailto:support@pogoda.in">

// support@pogoda.in</a>

$text = ereg_replace('[-a-z0-9!#$%&\'*+/=?^_`{|}~]+@([.]?[a-zA-Z0-9_/-])*',

'<a href="mailto:\\0">\\0</a>',$text);// convert http://www.pogoda.in/new_york/eng/ into

// <a href="http://pogoda.in/new_york/eng/">

// pogoda.in/new_york/eng/</a>

$text = ereg_replace('[a-zA-Z]+://(([.]?[a-zA-Z0-9_/-])*)',

'<a href="\\0">\\1</a>',$text);

// convert www.pogoda.in/new_york/eng/ into

// <a href="http://www.pogoda.in/new_york/eng/">

// www.pogoda.in/new_york/eng/</a>

$text = ereg_replace('(^| )(www([-]*[.]?[a-zA-Z0-9_/-?&%])*)',

' <a href="http://\\2">\\2</a>',$text);

return $text;

}

Example

echo replaceLinks('mailto:tatump@memo8.com');
echo"<br />";
echo replaceLinks('http://www.memo8.com/toolkits/')

หรือจะเอาไปดัดแปลงเป็นการอ่านมาจากไฟล์ก็ได้นะครับ

ที่มา : http://th2.php.net/eregi_replace

ฟังก์ชั่น Regular expressions ของ PHP

บันทึกโดย TaTump วันที่ 29 December 2007 | 243 views
  1. ereg : ใช้ค้นหาคำที่มีในข้อความที่ต้องการ
  2. eregi : ใช้เหมือนกับ ereg แต่ case sensitive
  3. ereg_replace : ใช้ค้นหาคำแล้วแทนที่ด้วยคำที่กำหนด
  4. eregi_replace : ใช้เหมือนกับ eregi แต่ case sensitive
  5. split : ใช้ค้นหาคำที่กำหนด และคืนค่าในลักษณะ array
  6. spliti : ใช้เหมือนกับ split แต่ case sensitive