|
2 May 2003, 17:07
|
#1
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
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.
|
|
|
2 May 2003, 17:18
|
#2
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
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.
|
|
|
2 May 2003, 17:22
|
#3
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
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?
|
|
|
2 May 2003, 17:39
|
#4
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
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...
|
|
|
2 May 2003, 17:41
|
#5
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
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 & 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.
|
|
|
2 May 2003, 17:49
|
#6
|
ŻŻŻŻŻŻŻŻŻ
Join Date: May 2001
Location: Sept 2057
Posts: 1,813
|
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.
|
|
|
2 May 2003, 19:50
|
#7
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
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.
|
|
|
2 May 2003, 20:38
|
#8
|
Ngisne
Join Date: Jul 2001
Location: right here
Posts: 79
|
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
|
|
|
2 May 2003, 23:09
|
#9
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
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.
|
|
|
3 May 2003, 01:35
|
#10
|
Gubbish
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
|
Read the Vbulletin sources?
__________________
Gubble gubble gubble gubble
|
|
|
15 May 2003, 00:40
|
#11
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
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
|
|
|
10 Jul 2003, 10:49
|
#12
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
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
|
|
|
10 Jul 2003, 12:01
|
#13
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
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/
|
|
|
10 Jul 2003, 18:39
|
#14
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
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
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
|
All times are GMT +1. The time now is 13:19.
| |