[thelist] php ereg_replace help needed

Kae Verens kverens at contactjuggling.org
Thu Mar 6 04:10:01 CST 2003

Dunstan Orchard wrote:
> 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?

Here's what I use (your code was a bit too complex for this time in the
morning) - haven't had any problems yet. try it with a string of
different URIs in a variable $a.

// take care of URIs
// if the URI is something like [www.a.place|description], show the word
"description", linked to "www.a.place"
$a=preg_replace("/\[<a([^>]*>)[^<]*<\/a>\|(.*?)\]/" , "<a
target=\"_top\"\\1\\2</a>" ,$a);
// internal anchors (#name) and local files

> [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]'

to convert the above to do that, replace this:
with this:

(the second version is without a '.')

> Is that possible?
> [3] Would it be better to use preg_replace instead of ereg_replace?

not sure about "better".

Kae  Verens +----------------------------------+ webworks.ie
        pay  |      http://www.webworks.ie      |  bee
    play     |  http://www.contactjuggling.org  |     boss
kae         |http://kverens.contactjuggling.org|         god

More information about the thelist mailing list