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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.