Jump to content

BBCode assistance needed


Matpatnik

Recommended Posts

Hi guys, Here is my problem, I'm using BBCode to format my $Text but the breaking line doesn't work it suppose to show <br /> instead of <br>The $Text is from a database, here is the code:

function BBCode($Text) {// Replace any html brackets with HTML Entities to prevent executing HTML or script	// Don't use strip_tags here because it breaks [url] search by replacing & with amp	$Text = str_replace("<", "<", $Text);	$Text = str_replace(">", ">", $Text);	// Convert new line chars to html <br /> tags	$Text = nl2br($Text);	// Set up the parameters for a URL search string	$URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";	// Set up the parameters for a MAIL search string	$MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@";	// Perform URL Search	$Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '<a href="$1" target="_blank">$1</a>', $Text);	$Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);	$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);	// Perform MAIL Search	$Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '<a href="mailto:$1">$1</a>', $Text);	$Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);		 	// Check for bold text	$Text = preg_replace("(\[b\](.+?)\[\/b])is",'<span class="bold">$1</span>',$Text);	// Check for Italics text	$Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<span class="italics">$1</span>',$Text);	// Check for Underline text	$Text = preg_replace("(\[u\](.+?)\[\/u\])is",'<span class="underline">$1</span>',$Text);	// Check for strike-through text	$Text = preg_replace("(\[s\](.+?)\[\/s\])is",'<span class="strikethrough">$1</span>',$Text);	// Check for over-line text	$Text = preg_replace("(\[o\](.+?)\[\/o\])is",'<span class="overline">$1</span>',$Text);	// Check for colored text	$Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<span style=\"color: $1\">$2</span>",$Text);	// Check for sized text	$Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1px\">$2</span>",$Text);	// Check for list text	$Text = preg_replace("/\[list\](.+?)\[\/list\]/is", '<ul class="listbullet">$1</ul>' ,$Text);	$Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is", '<ul class="listdecimal">$1</ul>' ,$Text);	$Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s", '<ul class="listlowerroman">$1</ul>' ,$Text);	$Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s", '<ul class="listupperroman">$1</ul>' ,$Text);	$Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s", '<ul class="listloweralpha">$1</ul>' ,$Text);	$Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s", '<ul class="listupperalpha">$1</ul>' ,$Text);	$Text = str_replace("[*]", "<li>", $Text);	// Check for font change text	$Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);	// Declare the format for [code] layout	$CodeLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">					 <tr>					   <td class="quotecodeheader"> Code:</td>					 </tr>					 <tr>					   <td class="codebody">$1</td>					 </tr>				   </table>';	// Check for [code] text	$Text = preg_replace("/\[code\](.+?)\[\/code\]/is","$CodeLayout", $Text);	// Declare the format for [quote] layout	$QuoteLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">					  <tr>						<td class="quotecodeheader"> Quote:</td>					  </tr>					  <tr>						<td class="quotebody">$1</td>					  </tr>					</table>';	// Check for [code] text	$Text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","$QuoteLayout", $Text);	// Images	// 	$Text = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="$1">', $Text);	// [img=widthxheight]image source[/img]	$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '<img src="$3" height="$2" width="$1">', $Text);	return $Text;}

I tried to use $Text = str_replace(array("\r\n", "\r", "\n"), "<br />", $Text); but it doesn't work.I do have an other problem, is I don't know how to avoid the breaking line in the list. Do I have to create a filter before insert it in the database?

Link to comment
Share on other sites

This shows an <br> instead of <br />?Because I use the same bbCodes, and they show <br /> and not <br>I got mine at bbcode.org, like I see you did.Just try to copy in mine:

<?php   //Local copy   function BBCode($Text)	   {		 // Replace any html brackets with HTML Entities to prevent executing HTML or script			// Don't use strip_tags here because it breaks [url] search by replacing & with amp			$Text = str_replace("<", "<", $Text);			$Text = str_replace(">", ">", $Text);			// Convert new line chars to html <br /> tags			$Text = nl2br($Text);			// Set up the parameters for a URL search string			$URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";			// Set up the parameters for a MAIL search string			$MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@";			// Perform URL Search			$Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '<a href="$1" target="_blank">$1</a>', $Text);			$Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);		 //$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);			// Perform MAIL Search			$Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '<a href="mailto:$1">$1</a>', $Text);			$Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);		 			// Check for bold text			$Text = preg_replace("(\[b\](.+?)\[\/b])is",'<span class="bold">$1</span>',$Text);			// Check for Italics text			$Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<span class="italics">$1</span>',$Text);			// Check for Underline text			$Text = preg_replace("(\[u\](.+?)\[\/u\])is",'<span class="underline">$1</span>',$Text);			// Check for strike-through text			$Text = preg_replace("(\[s\](.+?)\[\/s\])is",'<span class="strikethrough">$1</span>',$Text);			// Check for over-line text			$Text = preg_replace("(\[o\](.+?)\[\/o\])is",'<span class="overline">$1</span>',$Text);			// Check for colored text			$Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<span style=\"color: $1\">$2</span>",$Text);			// Check for sized text			$Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1px\">$2</span>",$Text);			// Check for list text			$Text = preg_replace("/\[list\](.+?)\[\/list\]/is", '<ul class="listbullet">$1</ul>' ,$Text);			$Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is", '<ul class="listdecimal">$1</ul>' ,$Text);			$Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s", '<ul class="listlowerroman">$1</ul>' ,$Text);			$Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s", '<ul class="listupperroman">$1</ul>' ,$Text);			$Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s", '<ul class="listloweralpha">$1</ul>' ,$Text);			$Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s", '<ul class="listupperalpha">$1</ul>' ,$Text);			$Text = str_replace("[*]", "<li>", $Text);			// Check for font change text			$Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);			// Declare the format for [code] layout			$CodeLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">								<tr>									<td class="quotecodeheader"> Code:</td>								</tr>								<tr>									<td class="codebody">$1</td>								</tr>						   </table>';			// Check for [code] text			$Text = preg_replace("/\[code\](.+?)\[\/code\]/is","$CodeLayout", $Text);			// Declare the format for [quote] layout			$QuoteLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">								<tr>									<td class="quotecodeheader"> Quote:</td>								</tr>								<tr>									<td class="quotebody">$1</td>								</tr>						   </table>';					 			// Check for [code] text			$Text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","$QuoteLayout", $Text);		 			// Images			// 			$Text = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="$1">', $Text);		 			// [img=widthxheight]image source[/img]			$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '<img src="$3" height="$2" width="$1">', $Text);		 		   return $Text;	  }?>

Link to comment
Share on other sites

Nope it does the same thingMaybe I did something wrong in this part!

while($nrow = mysql_fetch_array($nresult)) {	if ($nrow['news_date'] == $nrow['news_lastmod']) {		$lastmod = "";	} else {		$lastmod = "Last modification: " . $nrow['news_lastmod'];	}	echo "<div class=\"story\">\n";	echo "<h4>" . $nrow['news_title'] . "</h4>\n<p>\n";	echo BBCode($nrow['news_info']) . "<br />\n";	echo "<span class=\"small\">" . $nrow['news_date'] . " " . $lastmod . "</span>\n";	echo "</p>\n</div>\n";}echo "</div>\n";

here is the link here

Link to comment
Share on other sites

echo BBCode($nrow['news_info']) . "<br />\n";I'm not sure if this goes right...echo bbcode($nrow['news_info']);echo "<br />";I think you need to do that. And you should strip slashes too, so you don't get several backslashes in front of these ' and stuff.echo stripslashes(bbcode($nrow['news_info'])); // Try this instead,echo "<br />";

Link to comment
Share on other sites

I did add the stripslashes() function and I find my mistake.I know what I was doing wrong, well I think, probably the breaking line was fine from the beginning. I'm using FF2 Web Developer add-on to have a quick look on the code instead of looking through the whole source and looking line by line where is the code... anyway by doing that the browser didn't look at the Doctype it just show the code where I highlight it so in this case it will show <br> as a breaking line. I just realize that :) does it make sense?

Link to comment
Share on other sites

Just a suggestion. Arrays would speed up the replace process also save lines. Use arrays to store the before and after effects of the tags.

$bbcode = array('[b]' => '<b>', '[/b]' => '</b>');foreach($bbocode as $tag => $html){$text = str_replace($tag, $html, $text);}

That will make it easier for you to insert and parse code.

Link to comment
Share on other sites

This line:$Text = nl2br($Text);Replaces line breaks with <br> tags. In versions of PHP prior to 4.0.5, nl2br replaces with <br>, after that it replaces with <br />. So your version of PHP is fairly old, 4.0.5 is several years old now. It would be best to upgrade PHP to a current version, but if you can't do that then you can replace the nl2br line with this:$Text = str_replace(array("\r\n", "\n", "\r"), "<br />", $Text);If you want to try to get around having line breaks at the end of <li> tags, then you can strip them before you do the replace. You would want to replace "<newline>[*]" with "[*]" and replace "<newline>[\list]" with "[\list]" before you replace the newlines. By <newline> I mean any of \r\n, \n, or \r. I don't want to give the code for that though, because the logic for creating a list isn't right in the first place. It does not add on the </li> tags, it should be doing that.

Link to comment
Share on other sites

  • 4 weeks later...

I just comeback from vacation, and did a painful reading of my code and others, good thing the comment exist :) , I'm back into it nowXenon Design, I'm not sure to understand your code. I did a quick reading about regex and I come out with this. I did change this line $Text = str_replace("[*]", "<li>", $Text); for this $Text = preg_replace("`\[\*\](.+?)\[\/\*\]`", '<li>$1</li>' ,$Text); so this is fixed.

This line:$Text = nl2br($Text);Replaces line breaks with <br> tags. In versions of PHP prior to 4.0.5, nl2br replaces with <br>, after that it replaces with <br />. So your version of PHP is fairly old, 4.0.5 is several years old now. It would be best to upgrade PHP to a current version, but if you can't do that then you can replace the nl2br line with this:
My php version is fine, it juste because I was using a selective view source so the nl2br() was not able to show it properly... anyway I'm not totally sure about what I'm saying :) . But I know for sure that when I viewing the whole source from the page (not just a part) it showing it just fine.
If you want to try to get around having line breaks at the end of <li> tags, then you can strip them before you do the replace. You would want to replace "<newline>[*]" with "[*]" and replace "<newline>[\list]" with "[\list]" before you replace the newlines. By <newline> I mean any of \r\n, \n, or \r. I don't want to give the code for that though, because the logic for creating a list isn't right in the first place. It does not add on the </li> tags, it should be doing that.
what do you mean? can you show me an example plz
Link to comment
Share on other sites

  • 2 weeks later...

I've fixed the breaking line in between each list with this:

$Text = preg_replace("`\[list\]\r\n(.+?)\[\/list\]`is", "<ul class=\"listbullet\">$1</ul>" ,$Text);	$Text = preg_replace("`\[list=1\]\r\n(.+?)\[\/list\]`is", "<ul class=\"listdecimal\">$1</ul>" ,$Text);	$Text = preg_replace("`\[list=i\]\r\n(.+?)\[\/list\]`is", "<ul class=\"listlowerroman\">$1</ul>" ,$Text);	$Text = preg_replace("`\[list=I\]\r\n(.+?)\[\/list\]`is", "<ul class=\"listupperroman\">$1</ul>" ,$Text);	$Text = preg_replace("`\[list=a\]\r\n(.+?)\[\/list\]`is", "<ul class=\"listloweralpha\">$1</ul>" ,$Text);	$Text = preg_replace("`\[list=A\]\r\n(.+?)\[\/list\]`is", "<ul class=\"listupperalpha\">$1</ul>" ,$Text);	$Text = preg_replace("`\[\*\](.+?)\[\/\*\]\r\n`", "<li>$1</li>" ,$Text);

Now I have to figure why I got this error on the validation

Line 121 column 26: document type does not allow element "ul" here; missing one of "object", "applet", "map", "iframe", "button", "ins", "del" start-tag.<ul class="listlowerroman"><li>Text</li><li>Text</li><li>Text</li><li>Text</li><The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element. One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>").
According to w3schools it look that I can use <ul> or <ol> with a class http://www.w3schools.com/tags/tag_ul.asp Anyone can give me an hint?Thank you
Link to comment
Share on other sites

The <ul> is a block-level element, and you have it inside of a <p>, which is an inline element. Removing the <p> tags surrounding the <ul> makes it validate. Use <div> instead of <p>, there's not much point to the <p> tag.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...