Jump to content

[SOLVED] Convert a non-linked url into a hyperlink.


JasonLewis

Recommended Posts

Howdy.

This is driving my insane. I just can't think anymore.

Okay I am trying out my regular expression knowledge by creating my own BBCode Parser. Now, I have it parsing stuff in url tags, similar to what is used on this website.

The thing is, I want to convert text written as www.website.com or http://website.com or http://www.website.com or website.com into active links. The problem isn't doing it, it's re-linking the ones that are already linked.

Basically, I only want to do it to text that isn't already linked.

 

Is there any pattern I can use that will allow me to do this?

 

Cheers all!

Okay this is the expression I'm using. It's working, except if the link is like this:

[url=www.website.com]some text[/url]

 

Or anything as long as the middle text has a space, this expression:

 

preg_replace(%
	### Not preceded by an url start.
	(?<!url[=]])
	(
		### Protocol or start.
		(?:
			(??:https?|ftp)://)
			|
			www.
		)
		### Body.
		(?>
			### Gobble all non-space with the exception of [/url]
			(??!\[/url\])\S)+
			### Avoid ending punctuation.
			(?<!\p{P})
		)
		### Not followed by an url end.
		(?!\[/url\])
	)
%x', '<a href="$1">$1</a>', $data);

 

Will match it.

 

How can I make it so that it won't match it when the middle text has spaces?

 

Cheers.

 

EDIT: I just removed the \S. Just let me know if that's an appropriate solution of if there is something better. Thanks.

Oddly enough, the code that is displayed is not the same as the code entered. Try this:

echo preg_replace('%

### Not preceded by an url start.

(?<!url[=\]])

(

### Protocol or start.

(?:

(?:(?:https?|ftp)://)

|

www\.

)

### Body.

(?>

### Gobble all non-space with the exception of [/url]

(?:(?!\[/url\])\S)+

### Avoid ending punctuation.

(?<!\p{P})

)

### Not followed by an url end.

(?!\[/url\])

)

%x', '<a href="$1">$1</a>', $data);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.