|
Posted by malatestapunk on 11/21/06 07:31
so many sites so little time wrote:
> acutally what krustov said is more of what i want, as long as they
> write out the link http:// then it should work well.
> so many sites so little time wrote:
> > yeah i guess i should get into regex, basicly i should prob just make a
> > button like most message boards do out of javascript to let them make
> > it a link i think wordpress does it that way to... blah off to school
> > Geoff Berrow wrote:
> > > Message-ID: <MPG.1fcbc0a16d76d94498a1e5@news.newsreader.com> from
> > > Krustov contained the following:
> > >
> > > >> or having to do <href = or <a href = for a link
> > > >> what could i do to have php enter to mysql or retrive it formated?
> > > >>
> > > >
> > > >If you mean you have a piece of bare text like http://www.whatever.com
> > > >and you want to make it into a hyperlink after grabbing it from your
> > > >database - then you could do something like the following .
> > > >
> > > ><?php
> > > >
> > > >$grab="http://www.whatever.com";
> > > >
> > > >$extra="<a href=$grab>$grab</a>";
> > > >
> > > >print $extra;
> > > >
> > > >?>
> > >
> > > It could be that he wants to turn anything that just looks like a link
> > > into a link. That would involve a bit of regex (note to self: must put
> > > in effort to learn more regex...)
> > >
> > >
> > >
> > > --
> > > Geoff Berrow (put thecat out to email)
> > > It's only Usenet, no one dies.
> > > My opinions, not the committee's, mine.
> > > Simple RFDs http://www.ckdog.co.uk/rfdmaker/
If you want to search plain text for valid urls and replace them with
links, here is a regex that may help you out:
<?php
$rx = '
{
\b
# Match the leading part (proto://hostname, or just hostname)
(
# ftp://, http://, or https:// leading part
(ftp|https?)://[-\w]+(\.\w[-\w]*)+
|
# or, try to find a hostname with our more specific
sub-expression
(?i: [a-z0-9] (?:[-a-z0-9]*[a-z0-9])? \. )+ # sub domains
# Now ending .com, etc. For these, require lowercase
(?-i: com\b
| edu\b
| biz\b
| gov\b
| in(?:t|fo)\b # .int or .info
| mil\b
| net\b
| org\b
| [a-z][a-z]\b # two-letter country codes
)
)
# Allow an optional port number
( : \d+ )?
# The rest of the URL is optional, and begins with / . . .
(
/
# The rest are heuristics for what seems to work well
[^;"\'<>()\[\]{}\s\x7F-\xFF]*
(?:
[.,?]+ [^;"\'<>()\[\]{}\s\x7F-\xFF]+
)*
)?
}x';
$text = "blah blah http://www.google.com, text text more text etc";
echo preg_replace ($rx, '<a href="\1">\1</a>', $text);
?>
Navigation:
[Reply to this message]
|