Closed Thread Icon

Topic awaiting preservation: Format URL in text string function.. (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8275" title="Pages that link to Topic awaiting preservation: Format URL in text string function.. (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Format URL in text string function.. <span class="small">(Page 1 of 1)</span>\

 
pwhitrow
Bipolar (III) Inmate

From: UK
Insane since: Feb 2002

posted posted 08-23-2002 17:12

Hi all,

I'm up against a deadline to get an application completed, so...

does anyone have a ready made function that I could 'borrow', that looks for all occurences of a url within a text string, wraps anchor tags around the url, and returns the formatted text string ..?

example:
inputstr='text http://www.url.com text text https://www.url.com text text www.url.com text'
outputstr='text <a href="http://www.url.com">http://www.url.com<a> text text <a href="https://www.url.com">https://www.url.com</a> text text <a href="www.url.com">www.url.com</a> text'

I'm really up against it guys, so I would appreciate any help...

Paul Whitrow



www.pwhitrow.com

andy_j
Nervous Wreck (II) Inmate

From: uk
Insane since: Aug 2002

posted posted 08-23-2002 20:16

Hi Paul

I knocked this up quickly, but dont know if its the sort of thing you are after or not

Example Here

I have only set it to replace http://www.XXXX.com
http://www.XXXX.org
http://www.XXXX.co.uk

but you could easily mod it. Also will only work if the url is surrounded by a space..



[This message has been edited by andy_j (edited 08-23-2002).]

pwhitrow
Bipolar (III) Inmate

From: UK
Insane since: Feb 2002

posted posted 08-23-2002 20:49

Thanks for the reply...I'll check it out and let you know how I get on..


www.pwhitrow.com

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-23-2002 21:08

Very cool! I was just thinking about this the other day. My only question is what needs to change in the regex to take full URLs?
For instance, http://www.xxx.com works fine, but http://www.xxx.com/index.php?a=1&b=2&c=3 won't (it just skips right over - no fatal errors).

I suck at regexs and could use the help.

andy_j
Nervous Wreck (II) Inmate

From: uk
Insane since: Aug 2002

posted posted 08-23-2002 21:28

you could mod the regexp to something like

/^(http:

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-23-2002 21:40

Okay, but that does just the opposite of the first. It resolves my question, but breaks the first method...... It doesn't create the <a> for shorter, conventional URLs. http://www.xxx.com doesn't work, while http://www.xxx.com/blah.php?a=1&b=2&c=3 does work. Ideally, shouldn't it create both links?



[This message has been edited by Pugzly (edited 08-23-2002).]

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-23-2002 21:42

I know you can REPLACE EVERYTHING FROM 'http:' or 'https:' or 'ftp:' or 'www.' TO (the next space char)

and put anchor tags around it..... I'll show you what I'm talking about, gimme a couple of hours to pull it off...


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-23-2002 21:53

Okay - I just got to thinking about this as well:

The www. part shouldn't be required. So, the first regexp could be like so:

var reg = /^(http:

andy_j
Nervous Wreck (II) Inmate

From: uk
Insane since: Aug 2002

posted posted 08-23-2002 22:20

i took so long to get my brain round that last bit that Pugzly has already soved it.

anyway, here is my final offering

var reg = /^(http:

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-23-2002 22:37

here's the string explained for the rest of us...

<BLOCKQUOTE><FONT face="Verdana, Arial">code:</font><HR><pre>
/
^ # Beginning of data string
(http:

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-24-2002 00:13

Well, not to be outdone, I thought - what about pages ending in php3 or shtm or variants of the existing allowed extensions?

I *THOUGHT* by using something like php* or php? it would hit on 'php' AND 'php3'. But that's not working. Here is my existing expression:

var reg = /^(http:

andy_j
Nervous Wreck (II) Inmate

From: uk
Insane since: Aug 2002

posted posted 08-24-2002 00:36

I think you would need to separate for example the php and 3 into its own group withing the extensions.

This is what i have come up with. It also fixes the problem of using http://www.aaa.com/index.html and http://www.aaa.com/otherdirectory/index.html

which didnt seem to work before in my earlier attempts

var reg = /^(http:

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-24-2002 17:12

see I know that '.php3?' matches either '.php' or '.php3'.. but I'm still working on my 'everything up to the next space' regexp... but I can't get it to wok for some reason...


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-24-2002 17:26

hah! got it... (it was deceptively simple)

this will match absolutely anything starting with "http (or https)://www. (or not on the 'www.')" and anything AT ALL until the next space.

<B>/^\b(http:

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-24-2002 17:40

since Pugzly asked.... here's the explanation for that one....

<B>/^\b(http:

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 08-24-2002 20:12

nice thread ya'll



.:[ Never resist a perfect moment ]:.

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 08-24-2002 20:50

Love this thread! Never really gotten the hang of those regexp's, but at least I understood the ones here thanks to those wonderfull explantions

_________________________
Anyone who has lost track of time when using a computer knows the propensity to dream, the urge to make dreams come true and the tendency to miss lunch.
- copied from the wall of cell 408 -

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-24-2002 21:48

there... used a little UBB to make the explanations a little more 'Aslyum Friendly'



Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-25-2002 01:57

Hmmm, ok.... I won't be outdone, Petskull!

Actually, to add another regex to this, I was thinking about checking if the URL was local. If it is, just print the <a> and be done. If it isn't, add a target="_blank" to the <a>.

Putting everything together, I come up with this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>wrapper</title>
<script type="text/javascript" language="javascript">
var reg = /^\b(http:

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-25-2002 10:35

<script language="javascript">
var reg = /^\b(http:

andy_j
Nervous Wreck (II) Inmate

From: uk
Insane since: Aug 2002

posted posted 08-25-2002 18:06

Hey nice work.. I was the same with those regex's, i hated them before this thread, but kinda understand them now.

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-25-2002 19:01

here, I'm going to write a little reference

hold on..

*door creaks open*
ok, I'm back... check this out..

--Wildcards--
. -=means=- any single character
* -=means=- zero (0) or more of the character that came before
+ -=means=- one (1) or more of the character that came before
? -=means=- zero (0) or one (1) of the character that came before
() -=means=- a group
[] -=means=- any single character in the set
[^] -=means=- any single character not in the set
{min,max} -=means=- number of times the previous should appear
--Character Clases--
\d -=means=- any digit
\D -=means=- any non-digit
\w -=means=- alphanumeric
\W -=means=- non-alphanumeric
\s -=means=- space- including [\t\n\r\f]
\S -=means=- non-space
--Anchors--
^ -=means=- the beginning
$ -=means=- the end
\b -=means=- the boundry of a word
\B -=means=- anyplace except the boundry of a word

what do you think? anything I didn't thoroughly explain?

explaining these is kinda fun... who needs examples?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

[This message has been edited by Petskull (edited 08-25-2002).]

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 08-25-2002 20:42

Petskull, after this post, you are my new hero. Why have I never "gotten" RegEx? Since 1994, my hard core programming buds lay this on me, "Oh simple!" they say, you just "[insert a bunch of incomprehensible characters here]"! Expect some of this stuff to creep into the project I'm working on right this minute.

Your pal, -doc-

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-25-2002 21:00

Looking at your regexp there,

/^\b(http:

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-25-2002 21:14

remember:

$stuff = m/[incomprehensible ]/i

matches [incomprehensible], while-

$stuff = s/[incomprehensible]/[more incomprehensible]/i

replaces [incomprehensible] with [more incomprehensible]...

slime, remember that in these examples we're splitting the whole text by space- in other words, each word is being treated seperately..



Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

[This message has been edited by Petskull (edited 08-25-2002).]

pwhitrow
Bipolar (III) Inmate

From: UK
Insane since: Feb 2002

posted posted 08-27-2002 09:33

Guys,

You rock...

Thanks for such a comprehensive response to what I thought was a rather bone question.

To the kings of Scripting.....long may you rule..

Paul

www.pwhitrow.com

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 04-24-2003 18:29

petskull wrote: /^\b(http:

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 04-25-2003 08:44

I think this should work, but you'll need to test for the 'www.' case so you can add the "http://"

/^\b(www.

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 04-25-2003 18:11

petSkull wrote: "way to resurrect a thread, by the way.."

Is that a positive comment or a negative one?

Sorry to 'raise the dead' as it were, but I like the idea of automating many of the dull tasks involved in maintaining websites and posting news etc. RegExp is very cool, I have been using it a lot lately but I only have the very basics, I haven't quite got the hang of anything more than a simple 'replace this character with this character' or 'remove the first 5 letters' etc.

Thanks for the response tho, It has got me going in the right direction.

visit my CryoKinesis Online Gallery

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 04-27-2003 05:43

by all means, that a great thing!

it's good to see that someone can use the damn 'search' button before starting a new thread. Not to mention, that this one of my all-time favorte threads..

if you need anything else, don't be afraid to ask- but kudos on searching to see if the question had already been answered- we need more of that around here..




Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 04-27-2003 13:06

well petskull my friend, I would appreciate your help on my other posts. As you may be aware I have been trying to create a horizontal thumbnail image scroller whose(it's not a person but it sounds better) direction and speed is controlled by the position of the mouse within the containing div. Something that has been used a lot in flash sites now but still remains a seemingly untouched area in dhtml. I know it can be done, and I have seen examples that use similar principles (the doc's landscape looping scroller thingy) but as Dracusis said in one of his replies it would better if I created it myslef that way I would understand what is happening and wouldn't have to deal with unecessary code. Trouble is my skills are none and my time little, so any segmented help anyone can give me would enable me to learn aswell as staying within my time constraints.

Thanks matey, I like this forum.

visit my CryoKinesis Online Gallery

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 04-27-2003 18:21

well, here's what I suggest-

start a new text file describing what exactly you're trying to do.. then break it down step by step

if you get stuck on any one step, the start a thread and ask questions about it. E.g "How do you get the mouse position over a div?" ...or whatever... when you know exactly what it is you DON'T know, it's a lot easier to help..


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

« BackwardsOnwards »

Show Forum Drop Down Menu