[thelist] php ereg_replace help needed
Kelly Hallman
khallman at wrack.org
Thu Mar 6 04:45:01 CST 2003
On Thu, 6 Mar 2003, Dunstan Orchard wrote:
>
> It does almost everything I want, with two exceptions:
> [1] if they miss off the 'http://' when inputing a url it doesn't insert
> the 'http' bit in the code and so the link doesn't work properly. <a
> href="www.1976design.com">www.1976design.com</a> instead of: <a
> href="http://www.1976design.com">www.1976design.com</a>
In my linking function I fixed this with a second regex replacement that
searches for href="www and alters it accordingly...
> [2] Is there a bit of code I can insert that stands for 'no character at
> all'. I want to catch instances where the user inputs a url immediately
> followed by a fullstop and then submits the form. I already catch
> fullstops followed by a space or a new line, but if I catch '.' then it
> messes up the fullstops in the actual url as well.
Maybe you are looking for \.$ or perhaps a positive lookahead (which you
probably need the preg_ function for) something like: \.(?=[\n\r\s])
(but don't quote me on that, or that even preg_ supports this)
> [3] Would it be better to use preg_replace instead of ereg_replace?
If ereg_replace does it for you, stick with it. Use the preg_ functions
only if you need regex functionality not supported by the ereg_ variants
For posterity, here is my URL linking function, which does well:
function clickable($t) {
// link URLs
$t = eregi_replace( "(([[:alnum:]]+://)|www\.)([^[:space:]]*)".
"([[:alnum:]#?/&=])", "<a href=\"\\1\\3\\4\" target=\"_blank\">".
"\\1\\3\\4</a>", $t);
// fix lazy URLs
$t = eregi_replace("href=\"www","href=\"http://www",$t);
// link mailtos
$t = eregi_replace( "(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)".
"([[:alnum:]-]))", "<a href=\"mailto:\\1\">\\1</a>", $t);
// truncates long urls that can cause display problems (optional)
$t = preg_replace("/>(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]".
"{30,40})([^[:space:]]*)([^[:space:]]{10,20})([[:alnum:]#?\/&=])".
"</", ">\\1\\3...\\5\\6<", $t);
return $t; }
--
Kelly Hallman
http://wrack.org/
More information about the thelist
mailing list