Closed Thread Icon

Topic awaiting preservation: Make your own bbCode (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12757" title="Pages that link to Topic awaiting preservation: Make your own bbCode (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Make your own bbCode <span class="small">(Page 1 of 1)</span>\

 
Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 05-22-2003 14:12

I know that a few asylumites over the years have made bbCode parsers, you know, things that will take a certain tag (like UBB code) and replace it with the appropriate HTML tag. Well the time has come when I need to do that and I cannot for the life of me get it to work. I had though just to do a recurring str_replace() that keeps searching through the text and each time it finds any of these tags it replacees it with it's HTML equivilant, but no...that doesn't seem to work...the damn tags are still there.

Help? Please?

___________________________

Skaarjj's ISP woes are not over yet...but I will return soon.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 05-22-2003 16:39

not sure what language you want, but here is a php script that seems it will do it.

Later,

C:\


~Binary is best~

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 05-22-2003 17:07

what do you know, I was working on this just last night in perl... here's what I got so far:

code:
#usr/bin/perl

$faked_file = "../fake.txt";
print "Content: text/html\n\n";

if (open(ARTINP,$faked_file)){
while(<ARTINP> ){
$cur_line = $_;
chomp($cur_line);
$cur_line =~ s/\[b\]/<b>/gi;
$cur_line =~ s/\[\/b\]/<\/b>/gi;
$cur_line =~ s/\[i\]/<i>/gi;
$cur_line =~ s/\[\/i\]/<\/i>/gi;
if ($cur_line !~ /^\s*$/i){
print "<p>\n";
print "$cur_line\n";
print "</p>\n";
}

}
} else {
print "Could not open: $faked_file.\n";
}

print "\n\n";



...more to come, obviously...


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 05-22-2003 17:17

This is what vBulletin does (with soe alterations by me to fit in with the GN):

<BLOCKQUOTE><FONT face="Verdana, Arial">code:</font><HR><pre> $searcharray = array(
"/(\[)(list)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/list)(((=)(\\4)([^\"']*)(\\4]))

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-22-2003 18:17

Theoretically what I would do (and on one project have done) is parse for anything within braces ([ ... ]) and then take the contents and check it against a list of possible replacements. If it fits then I'd do the replace. If it doesn't then I'd print out the brackets as is.



.:[ Never resist a perfect moment ]:.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 05-23-2003 00:30

Here's what I'm using most of which is from phpBB.

<BLOCKQUOTE><FONT face="Verdana, Arial">code:</font><HR><pre>
function ubb_to_html($description) {
/* Add some padding in case the first character is part of a URL */

$description = " " . $description;

// Make andthing that begins http:// or www. an <a> link


$description = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $description);


$description = preg_replace("#([\n ])([a-z]+?)://([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $description);


/* Make proper replacements for the UBB tags in description */

if ( (strpos($description, "[") && strpos($description, "]")) ) {

// Replace the [b] tags for <b>

$description = str_replace('', '<b>', $description);
$description = str_replace('
', '</b>', $description);

// Replace the [i] tags for <i>

$description = str_replace('', '<i>', $description);
$description = str_replace('
', '</i>', $description);

// Replace the

    tags for <list> and
  • for <li>



    $description = str_replace('[list]', '<list>', $description);

    $description = str_replace('

', '</list>', $description);

// Replace [li] for <li>

$description = str_replace('[li]', '<li>', $description);
$description = str_replace('[/li]', '', $description);

// Replace [color=] tags for <span style="color:#>

$description = preg_replace("/\[color=(\#[0-9A-F]{6}

Piper
Paranoid (IV) Inmate

From: California
Insane since: Jun 2000

posted posted 05-23-2003 03:34

Petskull: You might find it faster to read in the whole file and make one pass per regex instead of one pass per line for each regex. File I/O and expressions are expensive. Here is a sample of the code I use to read in a file:

code:
open HTML, "<$file" or die "Could not read-open $file. Reason: $!";
# flock (HTML, LOCK_EX) or die "Can't get an exclusive lock on $file: $!"; # Requires: [b]use Fcntl qw{ :flock };[/b] to work.
read (HTML, my $html, -s HTML);
close HTML;


# Do your regexes here on $html



You have to potential to eat up a lot of ram if your file gets huge, say, more than a few 10's of MB's. It also depends on how much ram you have on the system.

Sorry for going slightly OT but I thought that might help.

~Charlie

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 05-23-2003 07:58

actually *real* ubb code parsing is a lot more complicated than just a couple dozen regexps...
For example the code tag needs a stack, to accuratly push things up and pull things down that shall not be decoded by the rest of your ubb code...

Still, most of the basics can be covered with a few regexps.

I know, for I have writen the version for the faq myself, but also a more advanced version for another, not yet completly finished, project.

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 05-24-2003 08:13

Piper... smart stuff.. I did get a little caught on this:
read (HTML, my $html, -s HTML);

... so I'm going to assume it does this:
read everything from the filehandle HTML and shove it into a variable called $html for the length ("-s") of the file pointed to by the filehandle HTML

something like that? if so, it's a neat little function... a little embarassed that I didn't concieve the algorythm that way...


code:
#usr/bin/perl

$faked_file = "../fake.txt";
print "Content: text/html\n\n";

open ARTINP,"$faked_file" or die "Could not read-open $file. Reason: $!";
read (ARTINP, $cur_line, -s ARTINP);
chomp($cur_line);

# Eliminate HTML tags
$cur_line =~ s/</</gi;
$cur_line =~ s/>/>/gi;

# Put paragraph tags around paragraphs
$cur_line =~ s/\n/<\/p><p>/gi;
$cur_line =~ s/<p><\/p>/\n\n/gi;

# Strip paragraph tags from topics
$cur_line =~ s/<p>\[tc\]/\[tc\]/gi;
$cur_line =~ s/\[\/tc\]<\/p>/\[\/tc\]/gi;

# Strip paragraph tags from subtopics
$cur_line =~ s/<p>\[st\]/\[st\]/gi;
$cur_line =~ s/\[\/st\]<\/p>/\[\/st\]/gi;

# Make Bold text Bold
$cur_line =~ s/\[b\]/<b>/gi;
$cur_line =~ s/\[\/b\]/<\/b>/gi;

# Make Italic text Italic
$cur_line =~ s/\[i\]/<i>/gi;
$cur_line =~ s/\[\/i\]/<\/i>/gi;

# Make Topics into Headers
$cur_line =~ s/\[tc\]/<h1>/gi;
$cur_line =~ s/\[\/tc\]/<\/h1>/gi;

# Make Subtopics into subheaders
$cur_line =~ s/\[st\]/<h3>/gi;
$cur_line =~ s/\[\/st\]/<\/h3>/gi;

print "$cur_line\n";

close ARTINP;

print "\n\n";




Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

[This message has been edited by Petskull (edited 05-24-2003).]

« BackwardsOnwards »

Show Forum Drop Down Menu