Jump to content


[solved] - BBCode parser...


7 replies to this topic

#1 ericrcan

    Young Padawan

  • Members
  • Pip
  • 103 posts

Posted 26 January 2006 - 12:27 AM

Ok, i know someone is going to say look at another tutoiral or search, but i did..i found it, but thats not how i wanted it...how can i have it so that it will show up like the forum? the only thing i need, is code. my code i ahve now, just changes the text type, adn shows the text..i want it to look like the forums..anyone know how? thanks



this is how i want it =)

Edited by zYpher, 26 January 2006 - 12:27 AM.


#2 Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 26 January 2006 - 05:24 AM

function bbcode($string) {
$replace = array(
	'@\[(?i)b\](.*?)\[/(?i)b\]@si',
	'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
	'@\[(?i)code\](.*?)\[/(?i)code\]@si'
);

$replacements = array ('<b>\\1</b>',
'<a href="\\1" target="_blank">\\2</a>', 
'<div class="BBCode">Code:</div>
 <div class="BBCode"> <font color=orange>\\1</font></div>'
);

$string = preg_replace($replace, $replacements, $string);
return $string;

}

basically thats a way of doing it

looking at
 '@\[(?i)b\](.*?)\[/(?i)b\]@si',
the (?i) checks the CaSe of the word(s)

so if you dont care whether or not they use
[VAR]
blah
[/VAR]

or

[var]
blah
[/var]


or
[vAr]
blah
[/vAr]


it would still show it right.

#3 Stu

    Retired P2L Staff

  • Publishing Betazoids
  • PipPipPipPip
  • 1,761 posts
  • Gender:Male

Posted 26 January 2006 - 12:14 PM

if you had used the forum search, you might have found this topic which may have been of some help:

http://www.pixel2lif...showtopic=17761

but if you didnt, then there it is anyways.

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 26 January 2006 - 04:10 PM

To get the code to display more like how the forum has it, i'd change the code to something more like:
function bbcode($string){
	$replace = array('#\[b\](.*?)\[/b\]#si',
					 '#\[url=(.*?)\](.*?)\[/url\]#si',
					 '#\[code\](.*?)\[/code\]#si');

	$replacements = array ('<b>\\1</b>',
						   '<a href="\\1" target="_blank">\\2</a>', 
						   '<div class="BBCode_title">Code:</div>'
						  .'<div class="BBCode_code">\\1</div>');

	$string = preg_replace($replace, $replacements, $string);
	return $string;
}
I removed the (?i)'s for the reason discussed here, and changed the html a bit. Basically, i changed the class names so he could have 1 css style defined for what is the red bar above the code, and another for the box containing the code. I also removed the font tag, not just because it's recommended you not use it, but because the BBCode_code style could also define any font styles you want, as well as the display of the box.

#5 ericrcan

    Young Padawan

  • Members
  • Pip
  • 103 posts

Posted 27 January 2006 - 11:36 AM

thanks rc69! Thats exactly i was looking for..I knew it was something like that, but im still learning PHP...Now its just time to edit the CSS :blush: Thanks man!

Quote

if you had used the forum search, you might have found this topic which may have been of some help:

http://www.pixel2lif...showtopic=17761

but if you didnt, then there it is anyways.


Quote

Ok, i know someone is going to say look at another tutoiral or search, but i did..i found it, but thats not how i wanted it...


#6 Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 27 January 2006 - 09:01 PM

View Postrc69, on Jan 26 2006, 09:10 PM, said:

To get the code to display more like how the forum has it, i'd change the code to something more like:
function bbcode($string){
	$replace = array('#\[b\](.*?)\[/b\]#si',
					 '#\[url=(.*?)\](.*?)\[/url\]#si',
					 '#\[code\](.*?)\[/code\]#si');

	$replacements = array ('<b>\\1</b>',
						   '<a href="\\1" target="_blank">\\2</a>', 
						   '<div class="BBCode_title">Code:</div>'
						  .'<div class="BBCode_code">\\1</div>');

	$string = preg_replace($replace, $replacements, $string);
	return $string;
}
I removed the (?i)'s for the reason discussed here, and changed the html a bit. Basically, i changed the class names so he could have 1 css style defined for what is the red bar above the code, and another for the box containing the code. I also removed the font tag, not just because it's recommended you not use it, but because the BBCode_code style could also define any font styles you want, as well as the display of the box.

i gave him what i use... and mine works perfectly.

#7 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 28 January 2006 - 03:59 AM

Just thought I would pop my head in here.

<?php
function parse ($string) {
	$search = array(
	   '#\[([bB])](.+)\[/([bB])]#',
	   '#\[([iI])](.+)\[/([iI])]#',
	   '#\[([uU])](.+)\[/([uU])]#',
	   '#\[([cC][oO][dD][eE])](.+)\[/([cC][oO][dD][eE])]#',
	   '#\[([iI][mM][gG])](.+)\[/([iI][mM][gG])]#',
	   '#\[([uU][rR][lL])](.+)\[/([uU][rR][lL])]#',
	   '#\[([uU][rR][lL])=(.*)](.+)\[/([uU][rR][lL])]#',
	   '#\[([cC][oO][lL][oO][rR])=(red|green|blue)](.+)\[/([cC][oO][lL][oO][rR])]#',
	   '#\[([sS][iI][zZ][eE])=(1|2|3|4|5|6|7)](.+)\[/([sS][iI][zZ][eE])]#',
	);
	$replace = array(
	   '<b>$2</b>',
	   '<i>$2</i>',
	   '<u>$2</u>',
	   '<code>$2</code>',
	   '<img src="$2" />',
	   '<a href="$2">$2</a>',
	   '<a href="$2">$3</a>',
	   '<font color="$2">$3</font>',
	   '<font size="$2">$3</font>',
	);
	return preg_replace($search, $replace, $string);
}
?>
You can add more colors and different sizes in [color] and [size]

#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 January 2006 - 01:13 PM

This topic has already been solved, if you wish to discuss another problem, please start a new topic.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users