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

ลักษณะการทำงานก็คือจะสร้างไฮเปอร์ลิงค์ให้โดยอัตโนมัติ
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

You can leave a response, or trackback from your own site. Responses are currently closed, but you can trackback from your own site. You can skip to the end and leave a response. Pinging is currently not allowed. Both comments and pings are currently closed.
Powered by WordPress | Shop Free T-Mobile Phones Online | Thanks to Verizon Phones without Contract, MMORPG Games and Sell Car