[thelist] php ereg_replace help needed

Dunstan Orchard dunstan at 1976design.com
Thu Mar 6 03:54:04 CST 2003


Hi there,

[script at bottom of post]

wonder if anyone can cast an eye at this please, it's a wee script
for replacing urls with the code to turn them into clickable links in
users' posts.

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.

eg. it outputs:

<a href="www.1976design.com">www.1976design.com</a>

instead of:

<a href="http://www.1976design.com">www.1976design.com</a>

Can anyone figure out a way to alter this script to cope with that?

[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.

So what I want to do is tell it to look for '.[nothing else at all]'

Is that possible?

[3] Would it be better to use preg_replace instead of ereg_replace?

Thanks very much - dunstan

*******************************************
function url_to_link($string)
{
// change full stops and question marks that don't need to be linked
$string = ereg_replace("\?  ",  " question_mark  ", $string);
$string = ereg_replace("\?",  " question_mark_end", $string);
$string = ereg_replace("\?\r\n"," question_mark_new_line  ", $string);
$string = ereg_replace("\.  ",  " full_stop  ", $string);
$string = ereg_replace("\.\r\n"," full_stop_new_line  ", $string);

$string = eregi_replace("((ftp://)|(http://)|(www))(([a-z0-9.\-
])*)(@)?(([a-z0-9\.])*)(([:/])?)((([a-z0-9]*)(@?)/*)+([.a-z]{2,4}){1})?((\??[a-
z0-9\=]*)?)((\&[a-z0-9\=]*)?)(([\.a-z]){2,4})?",  "<a href=\"\\0\">\\0</a>",
$string);

// change back full stops and question marks
$string = ereg_replace(" full_stop_new_line  ",  "\.\r\n", $string);
$string = ereg_replace(" full_stop",  "\.  ", $string);
$string = ereg_replace(" question_mark_new_line  ",  "\?\r\n", $string);
$string = ereg_replace(" question_mark_end",  "\?", $string);
$string = ereg_replace(" question_mark",  "\?  ", $string);
return $string;
}
*******************************************


---------------------------
Dorset, England
http://www.1976design.com/
http://www.orchard.it/
http://www.maccaws.org/



More information about the thelist mailing list