User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 2 May 2003, 17:07   #1
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
PHP: Script that replaces BB-code tags to HTML

Does anyone have a script around that replaces BB-code tags (as used on these forums) to plain HTML.

Thus, one that converts [ url ] tags to <a href>'s and an [img] one that converts to <img src=>'s?

I'm quite crap with these PHP evaluators (not sure if you call them like that) so I need a tad of help with this.
Structural Integrity is offline   Reply With Quote
Unread 2 May 2003, 17:18   #2
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
I've looked through several sources (ie that of PHP nuke), but I'm unable to find it...

I'm very much a n00bie when it comes to these language specific evaluators/replacement functions.
Structural Integrity is offline   Reply With Quote
Unread 2 May 2003, 17:22   #3
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
OK, there are two types of vbcode replacement:
1. (b)param(/b)
2. (url=option)param(/url)

The first can be matched with something like
"\\[$tag\\](.*?)\\[/$tag\\]"
the second with something like
"\\[$tag=(.*?)\\](.*?)\\[/$tag\\]".
What's the hard part?
queball is offline   Reply With Quote
Unread 2 May 2003, 17:39   #4
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
OK, I'm getting somewhere now...
I've seen these methods with matching patterns in a small PERL course we had in school.

The only thing I don't understand yet is \\1 and \\2 etc etc... are these replaced with the old values in the matched pattern?

So, would this work?

Code:
$content = "my text.... [ url=http://www.blaat.com]blaat[/url ]";
$pattern = "\[url=(.*?)\](.*?)\[/url\]";
$replace = "<a href=\"\\1\">\\2</a>";
preg_replace($pattern,$replace,$content);
going to try this out now...
Structural Integrity is offline   Reply With Quote
Unread 2 May 2003, 17:41   #5
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Full example:
Code:
<?
$bbcode='[ b ]h[ /b ]e[ url=hello ]l[ /url ]lo';
$bbcode=htmlentities($bbcode);
$noparam=array('b'=>'<b>\\1</b>','url'=>'<a href="\\1\">\\1</a>');
foreach ($noparam as $tag=>$html){
$bbcode=preg_replace("#\\[$tag\\](.*?)\\[/$tag\\]#",$html,$bbcode);
}
$withparam=array('url'=>'<a href=\"\\1\">\\2</a>');
foreach ($withparam as $tag=>$html){
$bbcode=preg_replace("#\\[$tag=(.*?)\\](.*?)\\[/$tag\\]#",$html,$bbcode);
}
echo "$bbcode\n";
URLs should really be checked for protocol and stuff, this could be done with preg_replace(",...,e",...,...) if the matches are gonna be kept in text in an array or database.


Edit: vB doesn't escape &amp; which means I can do [b], but it's a bit weird...
Are you making a forum type thing? Will it have ?

Last edited by queball; 2 May 2003 at 17:48.
queball is offline   Reply With Quote
Unread 2 May 2003, 17:49   #6
xtothez
ŻŻŻŻŻŻŻŻŻ
 
xtothez's Avatar
 
Join Date: May 2001
Location: Sept 2057
Posts: 1,813
xtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud of
Slightly off topic, but I use this function in my IRC services to parse all output with mirc control codes from simple html tags (saves me from having to put ".chr(3)." in the middle of all my output text).

Code:
function parse_output($message)
{
	$patterns[0] = "/<b>/i";
	$replacements[0] = chr(2);
	
	$patterns[1] = "/<\/b>/i";
	$replacements[1] = chr(2);
	$patterns[2] = "/<u>/i";
	$replacements[2] = chr(31);
	$patterns[3] = "/<\/u>/i";
	$replacements[3] = chr(31);
	$patterns[4] = "/<k>/i";
	$replacements[4] = chr(3);
	$patterns[5] = "/<\/k>/i";
	$replacements[5] = chr(3);
	$patterns[6] = "/<k(1[0-5]|[0-9])>/i";
	$replacements[6] = chr(3) . "\$1";
	$patterns[7] = "/\040{2}/i";
	$replacements[7] = chr(160) . chr(160);
	$message = preg_replace($patterns, $replacements, $message);
	
	return $message;
}
__________________
in my sig i write down all my previous co-ords and alliance positions as if they matter because I'm not important enough to be remembered by nickname alone.
xtothez is offline   Reply With Quote
Unread 2 May 2003, 19:50   #7
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Regexps are the spawn of the devil.
It's just a shame they're so goddamn useful.

__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
meglamaniac is offline   Reply With Quote
Unread 2 May 2003, 20:38   #8
Atamur
Ngisne
 
Join Date: Jul 2001
Location: right here
Posts: 79
Atamur is an unknown quantity at this point
Quote:
Originally posted by meglamaniac
Regexps are the spawn of the devil.
It's just a shame they're so goddamn useful.

don't speak ill of regexps. they and other poorly readable constructs/languages are the core of programmers' job security.
__________________
down with signatures
Atamur is offline   Reply With Quote
Unread 2 May 2003, 23:09   #9
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
They're not hard to learn if you put your mind to it.
I went from not knowing a thing about them to being able to write one that validates URL syntax (not just checking for the http:// - checking the whole construct and any aditional path etc) in about 5 hours via a tutorial I found with my friend google.

However, I agree that they are incredibly boring, terrible to debug, and look very hard and impressive so yes you can sting people for cash with them i expect.

__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
meglamaniac is offline   Reply With Quote
Unread 3 May 2003, 01:35   #10
W
Gubbish
 
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
W is a jewel in the roughW is a jewel in the roughW is a jewel in the rough
Read the Vbulletin sources?
__________________
Gubble gubble gubble gubble
W is offline   Reply With Quote
Unread 15 May 2003, 00:40   #11
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Quote:
Originally posted by meglamaniac

However, I agree that they are incredibly boring, terrible to debug, and look very hard and impressive so yes you can sting people for cash with them i expect.

:)
Code:
#!/usr/bin/perl
$_="[url=http://somesite.com/]hello[/url]";
$tag = "url";
$proto = "http://";
/           #start regexp
\[          #start opening bbcode tag
  $tag      #look for tag name
  ={0,1}    #optional parameter
  (         #group the parameter
    $proto  #match valid protocol only
    [^[]*   #get the rest of the url
  )         #end grouping the parameter
  {0,1}     #only match parameter once or not at all
\]          #close opening bbcode tag
  (         #group enclosed text
    [^\[]*  #anything except a ]
  )         #end grouping it
\[          #look for end tag
  \/        #only want closing tags
  $tag      #tag name
\]          #close ending tag
/x;         #finish regexp
print "<a href='",$1,"' target='_blank'>",$2,"</a>\n";
Yes, pcre are so read only. God forbid some blame be placed at the people writing the regexp.
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 10 Jul 2003, 10:49   #12
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
bbcode

Who was the person trying to do bbcode in php?
Did you manage it?
If not, I've just done it myself, happy to share the juice.
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 10 Jul 2003, 12:01   #13
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Re: bbcode

Quote:
Originally posted by MT
Who was the person trying to do bbcode in php?
Did you manage it?
If not, I've just done it myself, happy to share the juice.
me... and yes... I've done it... And I understand regexps now.

\o/
Structural Integrity is offline   Reply With Quote
Unread 10 Jul 2003, 18:39   #14
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Re: bbcode

Quote:
Originally posted by MT
Who was the person trying to do bbcode in php?
Did you manage it?
If not, I've just done it myself, happy to share the juice.
MAD THREAD MERGING SKILLZ!

and, why couldnt i see this thread last night :/
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 13:19.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright İ2002 - 2018