Perl question

Author
Discussion

rpguk

Original Poster:

4,492 posts

295 months

Sunday 23rd November 2003
quotequote all
Hey all,

I'm currently building a little database backed web page.

Part of it needs 'friendly formatting' (basically something along the lines of formatting codes here)

The fixed things like replacing a carrige return with a <br> and a [bold] with <b> are easy, I'm using the following code.

$cont=~s/\n/<br>/g;

Problem comes with links and images, how would I do something like above but with a link. So they simply enter something like [link=whatever.com]This is what is displayed[/link] to be replaced with <a href="whatever.com">This is what is displayed</a>

If someone can post a quick answer it'd be a massive help.

Thanks.

Edited because I forgot about PH code might make this hard to post!

>>> Edited by rpguk on Sunday 23 November 00:15

>>> Edited by rpguk on Sunday 23 November 00:17

fatsteve

1,143 posts

288 months

Sunday 23rd November 2003
quotequote all
Richard,

This is a bit messy, but here goes:

$cont=~s/\[link/\<a href/g;
$cont=~s/\]/\>/g;
$cont=~s/\[/\</g;

The important thing you need to remmeber to use the \ break char whenever you reference the [ or ] chars, since Perl will try an evaluate it as a regex.

Steve

>> Edited by fatsteve on Sunday 23 November 01:24

>> Edited by fatsteve on Sunday 23 November 01:26

rpguk

Original Poster:

4,492 posts

295 months

Sunday 23rd November 2003
quotequote all
Sir Steve,

You are the man. I'm almost crying tears of joy, my poor brain couldn't think of that logical explaniation (I'll blame it on working since 10 this morning.)

Many Many Thanks.

fatsteve

1,143 posts

288 months

Sunday 23rd November 2003
quotequote all
Richard you are very welcome.

IMHO Perl is the bastard of all languages. My background is J2EE and also Unix sysadmin so my structured language skills and scripting (shell etc) skills are generally good. However, Perl always has me scratching my head!!

I'm trying again to learn it because I've got a SlimServer MP3 streamer (www.slimdevices.com) and the whole lot is open source so can be fettled with as your needs be.

Steve

cmt

14 posts

256 months

Monday 24th November 2003
quotequote all
I can't believe I registered just for this

here's a 1 line way of doing it....

@array = split(/([link=)|([)|(])/, "[link=whatever.com]This is what is displayed[/link]");

$array[4] now contains the url
$array[8] contains the text to display.

Col.


Sad I know

>> Edited by cmt on Monday 24th November 16:47

cmt

14 posts

256 months

Monday 24th November 2003
quotequote all
or using regexp

$_ = "[link=whatever.com]This is what is displayed[/link]";
($url , $urltext) = /^[link=(.*)](.*)[.*$/;

$url is the url from [link= ...]
$urltext is the link text.

>> Edited by cmt on Monday 24th November 16:58

>> Edited by cmt on Monday 24th November 17:00

rpguk

Original Poster:

4,492 posts

295 months

Monday 24th November 2003
quotequote all
cmt said:
I can't believe I registered just for this

here's a 1 line way of doing it....

@array = split(/([link=)|([)|(])/, "[link=whatever.com]This is what is displayed[/link]");

$array[4] now contains the url
$array[8] contains the text to display.

Col.


Sad I know

>> Edited by cmt on Monday 24th November 16:47


Nice one thanks!

And of course welcome to Pistonheads