Forum Moderators: coopster

Message Too Old, No Replies

Link Regex Problem

         

Nukem

6:45 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



Im trying to create a news posting script and i use

$changed_content = preg_replace ( "/\[url=(.*?)\](.*?)\[\/url\]/i", "<a href=\\1 target=_blank>\\2</a>", $changed_content );

to transfer something like:

[url=www.site.com]Site[/url] into <a href=www.site.com target=_blank>\Site</a>

The thing is, i want to change it back from HTML into TAGS so when people eidt the content it is done easily.

Can anyone help me out with?
Is the replace code im using a good one for what im goin?
Or anyone know where i find the code o do what im trying?

Thanks again

coopster

7:14 pm on Apr 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Nukem!

The regex looks like it should work...have you tested it? To change the links back, why not simply reverse the regex?

Nukem

10:54 am on Apr 17, 2004 (gmt 0)

10+ Year Member



Thanks!

Well the code does work fine, just wanted to make sure it was done ok.

But i cannot get it to reverse correctly its a pain ... im not so good with regex you see.

Elijah

11:35 am on Apr 17, 2004 (gmt 0)

10+ Year Member



The thing is, i want to change it back from HTML into TAGS so when people eidt the content it is done easily.

One way that you could do it is to store the bb-style tags in the database and only use preg_replace when actually displaying the news story. That way you won't have to convert back from HTML.

Elijah

Nukem

7:07 pm on Apr 17, 2004 (gmt 0)

10+ Year Member



ahh didnt think of that.

But i solved it in the end!

If anyone is interested its:
$changed_content = preg_replace ( "/\<a href='(.*?)' target='_blank'\>(.*?)\<\/a\>/i", "[url=\\1]\\2[/url]", $changed_content );