Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Converting to html tags...

by kiat (Vicar)
on Mar 10, 2004 at 16:29 UTC ( #335500=perlquestion: print w/replies, xml ) Need Help??

kiat has asked for the wisdom of the Perl Monks concerning the following question:


I would like to know if anybody has written or knows of any module or a simple sub that does the work of replacing tags like

[b]Text\[/b] [glow]Text[/glow]
in a block of text with their corresponding html tags
I need it for a small discussion board I'm coding and I'm hoping that there's a module or sub out there that I can depend on for the conversion.

Replies are listed 'Best First'.
Re: Converting to html tags...
by kvale (Monsignor) on Mar 10, 2004 at 16:45 UTC
    I do not know of any module that will do this specifically, but direct tag replacement could be accomplished with regexps:
    while (<FILE>) { s/\[b\]/<b>/g; s|\[/b\]|</b>|g; # etc. ... }


Re: Converting to html tags...
by Tomte (Priest) on Mar 10, 2004 at 17:07 UTC

    this is based on slashcodes html-cleaning sub, any error big or small is my fault. use with caution, may produce "transitional" html ;), extracts links and uses the hostname as link-text, allows links in the form

    needs URI.

    At least something to play with...


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

•Re: Converting to html tags...
by merlyn (Sage) on Mar 10, 2004 at 20:02 UTC
Re: Converting to html tags...
by Vautrin (Hermit) on Mar 10, 2004 at 17:26 UTC
    Although I'm not sure of any module per se, there are a bunch of Open Source (Perl and non perl based) Wiki / Forum / etc scripts. Perhaps you could use the code from one of these? (Assuming, of course, it is permissible)

    Want to support the EFF and FSF by buying cool stuff? Click here.
Re: Converting to html tags...
by PodMaster (Abbot) on Mar 11, 2004 at 00:24 UTC
Re: Converting to html tags...
by pbeckingham (Parson) on Mar 10, 2004 at 18:22 UTC
    For specifically the two tags shown (ignoring the stray backslash in the example), I would use:
    $text =~ s/\[(b|glow)\]/<$1>/g;
    But for a general case, if one even exists, I would use:
    $text =~ s/\[([^\]]+)\]/<$1>/g;
    But this is more dangerous, as those square brackets are in my opinion likely to occur in text.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://335500]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-06-26 07:14 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (84 votes). Check out past polls.