note 26709 added to function.ereg-replace
| From: | chalx#mailru dot com at rack1 dot php dot net | Date: | Sun, 10 Nov 2002 00:06:28 +0000 |
| Subject: | note 26709 added to function.ereg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-39245@lists.php.net to get a copy of this message | ||
Here is another one "Replace URLs with links" example. It is certainly wrong for some
small subset of cases, but it's much more usable than current manual example (causing bug even
used on this notes - see url in post #3 from 01-02-02).
For some read/write/performance reasons the expression was split into several variables. It's
sure possible to concatenate all in one string, but it gives nothing good.
$vdom = "[:alnum:]"; // Valid domain chars
$vurl = $vdom."_~-"; // Valid subdomain and path chars
$vura = "À-ßà-ÿ!#$%&*+,;=@.".$vurl; // Valid additional parameters (after
'?') chars;
// insert other local characters if needed
$protocol = "[[:alpha:]]{3,10}://"; // Protocol exp
$server = "([$vurl]+[.])*[$vdom]+"; // Server name exp
$path = "(([$vurl]+([.][$vurl]+)*/)|([.]{1,2}/))*"; // Document path exp (/.../)
$name = "[$vurl]+([.][$vurl]+)*"; // Document name exp
$params = "[?][$vura]*"; // Additional parameters (for GET)
$str = ereg_replace("$protocol$server(/$path($name)?)?($params)?", "<a
href=\"\\0\">\\0</a>", $str); // URL into links
--
http://www.php.net/manual/en/function.ereg-replace.php
http://master.php.net/manage/user-notes.php?action=edit+26709
http://master.php.net/manage/user-notes.php?action=delete+26709
http://master.php.net/manage/user-notes.php?action=reject+26709