Qt-interest Archive, March 2002
Finding URLs in text
Message 1 in thread
Hi, let's say I have a QString with some www.someurl.com and
http://someotherurl.net adresses. How do I efficiently and nicely
capture them, and convert into A HRef links? Has anyone done anything
like that before or is good enough in algorythms to come up with
an explanation? My code is very dirty (gotos) and buggy :(
TIA
--
[ signature omitted ]
Message 2 in thread
Am Thursday 21 March 2002 09:45 schrieb Tomek Jarzynka:
> Hi, let's say I have a QString with some www.someurl.com and
> http://someotherurl.net adresses. How do I efficiently and nicely
> capture them, and convert into A HRef links? Has anyone done anything
> like that before or is good enough in algorythms to come up with
> an explanation? My code is very dirty (gotos) and buggy :(
You might have a look at KSircView::addLine() here:
http://lxr.kde.org/source/kdenetwork/ksirc/ksview.cpp#L218
HTH,
--
[ signature omitted ]
Message 3 in thread
Hi,
I don't know, if my code is efficient or nice, but it seems to work:
QString text = "some text including urls like www.testsite.org,
http://whatever.net or http://www.irgendwas.de";
int s=0, p, l;
for (;;) {
// find next url
if (s >= (int)text.length()) { break; }
p = (text.find ("http://", s) > text.find ("www.", s)) ? text.find
("www.", s) : text.find ("http://", s);
if (p < 0) { break; }
// remove 'bad' chars like "," at end of url
l = (text.find (" ", p) < 0) ? text.length () - p : text.find (" ",
p) - p;
QChar c = text [l+p-1];
while ( !( ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9')) ) )
c = text [p-1+(--l)];
// add <a href=""> and </a> and set new startposition
text = text.left (p) + "<a href=\"" + text.mid (p, l) + "\">" +
text.mid (p, l) + "</a>" + text.mid (p+l);
s = p + 2*l + 15;
}
So my definition of an url is a text starting with "http://" or with
"www.".
Bye
Matthias Bartelt
Tomek Jarzynka wrote:
>
> Hi, let's say I have a QString with some www.someurl.com and
> http://someotherurl.net adresses. How do I efficiently and nicely
> capture them, and convert into A HRef links? Has anyone done anything
> like that before or is good enough in algorythms to come up with
> an explanation? My code is very dirty (gotos) and buggy :(
>
> TIA
>
> --
> tomasz k. jarzynka / 601 706 601 / tomee@.cpi.pl
>
> "And on the eigth day God said: "OK, Murphy, you take over." "
>
> --
> List archive and information: http://qt-interest.trolltech.com