BBcode-Tutorial

Hunter Greec

Mitglied
Hi Leute,
ich hab das Tutorial mal ausprobiert!
Es funktioniert auch... bis auf die Farben!

PHP:
<?php
error_reporting(E_ALL | E_NOTICE);

function parseBBCode2HTML( $bb )
{
    $bb = preg_replace('/\[ b \](.*?)\[\ /b \]/', '<b>$1</b>', $bb);
    $bb = preg_replace('/\[ i \](.*?)\[\ /i \]/', '<i>$1</i>', $bb);
    $bb = preg_replace('/\[ color=([[:alnum:]]{6}?).*\ ](.*?)\[\ /color \]/', '<font color="#$1">$2</font>', $bb);
    $bb = preg_replace('/\[ url=([^ ]+).*\ ](.*)\[\ /url \]/', '<a href="$1">$2</a>', $bb);

    $bb = preg_replace('/\n/', "<br/>\n", $bb);

    return $bb;

}
/* ACHTUNG! Die Foren-Software stellt bei color vor den Farbwert
   automatisch ein Raute-Zeichen, das war von mir nicht beabsichtigt.
   Bitte löscht beim Ausprobieren die Raute vor der Farbangabe raus. */
$text = "Dieser Teil soll fett gedruckt sein
[ i ]und dieser kursiv[ /i ].
[ color=black ]Hier wird eine Warnung in rot zu sehen sein.[ /color ]
Außerdem haben wir noch einen [ url=http://www.tutorials.de ]Link[ /url ].";

$formatiert = parseBBCode2HTML( $text );

echo $formatiert;
?>

Was ist daran nicht richtig... !?

Mfg Hg
 
Den Hinweis hast du auch beachtet?

ACHTUNG! Die Foren-Software stellt bei color vor den Farbwert
automatisch ein Raute-Zeichen, das war von mir nicht beabsichtigt.
Bitte löscht beim Ausprobieren die Raute vor der Farbangabe raus.
 
hallo leute

kann mir einer auch mal für den code den php-ausgabe code geben ich steig da iwi nicht ganz dahinter.

thx
 
ich post mal den bbcode von mir also den erweiterten mit dem code und die create für die ausgabe wenn das weiter hilft

PHP:
<?php
	
	function parseBBCode2HTML($msg) {
	
		$msg = $msg;
		
		$msg = preg_replace("/\[b\](.*?)\[\/b\]/", "<b>$1</b>", $msg);
		$msg = preg_replace("/\[i\](.*?)\[\/i\]/", "<i>$1</i>", $msg);
		$msg = preg_replace("/\[u\](.*?)\[\/u\]/", "<u>$1</u>", $msg);
		$msg = str_replace("/\[s\](.*?)\[\/s\]/", "<s>$1</s>", $msg);
		$msg = str_replace("/\[left\](.*)\[/left\]/", "<s>$1</s>", $msg);
		$msg = str_replace("/\[center\](.*)\[/center\]/", "<s>$1</s>", $msg);
		$msg = str_replace("/\[right\](.*)\[/right\]/", "<s>$1</s>", $msg);
		$msg = preg_replace('#\[url\](.*)\[/url\]#isU', "<a href=\"$1\">$1</a>", $msg);
		$msg = preg_replace('#\[ url=(.*)\](.*)\[/url\]#isU', "<a href=\"$1\">$2</a>", $msg);
		$msg = str_replace("/\n/", "<br/>\n", $msg);
		$msg = str_replace(":P","<img src=\"layout/smileys/000.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
		$msg = str_replace(":)","<img src=\"layout/smileys/001.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
		$msg = str_replace(";)","<img src=\"layout/smileys/002.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
		$msg = str_replace(":(","<img src=\"layout/smileys/003.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
		$msg = str_replace("*grr*","<img src=\"layout/smileys/004.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
		$msg = str_replace("o_o","<img src=\"layout/smileys/005.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
		$msg = str_replace("*_*","<img src=\"layout/smileys/006.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
		$msg = str_replace("._.","<img src=\"layout/smileys/007.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
		$msg = str_replace("-_-","<img src=\"layout/smileys/008.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
		$msg = str_replace(":D","<img src=\"layout/smileys/009.gif\" width=\"15\" height=\"14\" border=\"0\">",$msg); 
	
    return $msg;
	}

	function linkLenght($treffer) {

		$url = trim($treffer[1]);
		
		if(substr($url,0,7)!= 'http://')
		$url = "http://".$url;
		
		if(strlen(trim($treffer[2]))!=0)
		$linkname = $treffer[2];
		
		else
		
		$linkname = $treffer[1];
		
		if(strlen($linkname)>50 AND !substr_count(strtolower($linkname), '[ img]') AND !substr_count(strtolower($linkname), '[ /img]'))
		$linkname = substr($linkname, 0, 45-3)."...".substr($linkname, -5);
		$ergebnis = '<a href="' . $url . '" target="_blank">' . $linkname . '</a>';
	
	return $ergebnis;
	}

?>



PHP:
<?php

		echo '<table width="100%" cellpadding="5" class="maintable">
		<tr>
			<td><h1>Neues Thema erstellen</h1><br />';
		
		if($_SESSION['signed_in'] == FALSE) {
		
		echo '<p>' . $lang['you_need_to'] . ' <a href="signin.php">' . $lang['signin_s'] . '</a>. - ' . $lang['not_yet'] . ' <a href="signup.php">' . $lang['registered'] . '</a>?</p><br>';
		
			}else{
		
			if($_SERVER['REQUEST_METHOD'] != 'POST') {
		
			$sql = "SELECT
						cat_id,
						cat_name,
						cat_description
					FROM
						categories";
		
			$result = mysql_query($sql);
		
		if(!$result) {
		echo '<p>' . $lang['no_selection_db'] . '.</p><br>';
		
		}else{
		
		if(intval(mysql_num_rows($result)) == 0) {
		if($_SESSION['user_level'] == 1) {
		echo '<p>' . $lang['no_categories'] . '.</p><br />';
		
			}else{
			echo '<p>' . $lang['first_cat_creation'] . '.</p><br>';
			}
		
		}else{
		
		include_once('data/flood.php');
		
		echo '<form method="post" name="create" action="">
		<p><input type="text" class="fields" size="40" maxlength="40" name="topic_subject"> <strong>' . $lang['title'] . '</strong></p>
		<p><select name="topic_cat" class="fields">'; 
		
		while($row = mysql_fetch_assoc($result)) {
		echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
		}
		
		echo '</select> <strong>Kategorie</strong></p>';
		echo "<strong>" . $lang['message'] . "</strong><br /><textarea name=\"post_content\" rows=\"5\" cols=\"5\" class=\"fields_text\"></textarea><br>
		<p><a href=\"javascript:insert(':P','')\"><img border=\"0\" src=\"layout/smileys/000.gif\" width=\"15\" height=\"14\" alt=\"000\" /></a>
		<a href=\"javascript:insert(':)','')\"><img border=\"0\" src=\"layout/smileys/001.gif\" width=\"15\" height=\"14\" alt=\"001\" /></a>
		<a href=\"javascript:insert(';)','')\"><img border=\"0\" src=\"layout/smileys/002.gif\" width=\"15\" height=\"14\" alt=\"002\" /></a>
		<a href=\"javascript:insert(':(','')\"><img border=\"0\" src=\"layout/smileys/003.gif\" width=\"15\" height=\"14\" alt=\"003\" /></a>
		<a href=\"javascript:insert('*grr*','')\"><img border=\"0\" src=\"layout/smileys/004.gif\" width=\"15\" height=\"14\" alt=\"004\" /></a>
		<a href=\"javascript:insert('o_o','')\"><img border=\"0\" src=\"layout/smileys/005.gif\" width=\"15\" height=\"14\" alt=\"005\" /></a>
		<a href=\"javascript:insert('*_*','')\"><img border=\"0\" src=\"layout/smileys/006.gif\" width=\"15\" height=\"14\" alt=\"006\" /></a>
		<a href=\"javascript:insert('._.','')\"><img border=\"0\" src=\"layout/smileys/007.gif\" width=\"15\" height=\"14\" alt=\"007\" /></a>
		<a href=\"javascript:insert('-_-','')\"><img border=\"0\" src=\"layout/smileys/008.gif\" width=\"15\" height=\"14\" alt=\"008\" /></a>
		<a href=\"javascript:insert(':D','')\"><img border=\"0\" src=\"layout/smileys/009.gif\" width=\"15\" height=\"14\" alt=\"009\" /></a></p>
		

		
		<p><small>" . $lang['spam_protection_question'] . " *</small><br>
		<strong>" . $lang['sum_of'] . " " . $random1 . " + " . $code . " =</strong> <input size=\"5\" name=\"zip\" id=\"Spamschutz\" type=\"text\" class=\"fields\" /><input type=\"hidden\" name=\"zip2\" value=" . $rand_result . "></p>
		<p><input type=\"submit\" class=\"button\" value=\"" . $lang['create_topic_button'] . "\" alt=\"" . $lang['create_topic_button'] . "\"></p>
		</form>";
		}
		}
		
			}else{
		
			$query  = "BEGIN WORK;";
			$result = mysql_query($query);
		
		if(!$result) {
		echo '<p class="false">' . $lang['error_occurred'] . '.</p><br>';
			}else{
		
			if(sha1($_POST["zip"]) != $_POST["zip2"]) {
		
			echo '<br><p class="false">' . $lang['invalid_spam'] . '!</p>
			<p>[ <a href="javascript:history.back();">' . $lang['back'] . '</a> ]</p><br>';
		
		}else{
		
		if(isset($_POST['topic_subject'])) {
		if(trim($_POST['topic_subject']) == "") {
		$errors[] = '<p>' . $lang['enter_a_title'] . '.</p><br>';
		}
		
			}else{
			$errors[] = '<p>' . $lang['enter_a_title'] . '.</p><br>';
			}
		
		if(isset($_POST['post_content'])) {
		if(trim($_POST['post_content']) == "") {
		$errors[] = '<p>' . $lang['enter_a_text'] . '.</p><br>';
		}
		
			}else{
			$errors[] = '<p>' . $lang['enter_a_text'] . '.</p><br>';
			}
		
		if(!empty($errors)) {
		echo '<p class="false">' . $lang['not_filled_all'] . '</p>
		<p>[ <a href="javascript:history.back();">' . $lang['back'] . '</a> ]</p><br>';
		echo '<ul>';
		
		foreach($errors as $key => $value) {
		echo '<li>' . $value . '</li>'; 
		}
		echo '</ul>';
		
			}else{
		
			$sql = "INSERT INTO 
						topics(topic_subject,
						topic_date,
						topic_cat,
						topic_by)
					VALUES ('" . mysql_real_escape_string($_POST['topic_subject']) . "',
						NOW(),
						'" . mysql_real_escape_string($_POST['topic_cat']) . "',
						'" . $_SESSION['user_id'] . "'
						)";
		
			$result = mysql_query($sql);
		
		if(!$result) {
		
		echo '<p class="false">' . $lang['error_occurred'] . '.</p><br>' . mysql_error();
		$sql = "ROLLBACK;";
		$result = mysql_query($sql);
		
			}else{
		
			if(sha1($_POST["zip"]) != $_POST["zip2"]) {
		
			echo '<br /><p class="false">' . $lang['invalid_spam'] . '!</p>
			<p>[ <a href="create_topic.php">' . $lang['back'] . '</a> ]</p><br>';
		
		}else{
		
		$topicid = mysql_insert_id();
		
		$sql = "INSERT INTO
					posts(post_content,
					post_date,
					post_topic,
					post_by)
				VALUES ('" . mysql_real_escape_string($_POST['post_content']) . "',
					NOW(),
					'" . $topicid . "',
					'" . $_SESSION['user_id'] . "'
					)";
		
		$result = mysql_query($sql);
		
		if(!$result) {
		
		echo '<p class="false">' . $lang['couldnot_create_topic'] . '</p><br>' . mysql_error();
		echo "<script type=\"text/javascript\">showErrorMessage();</script>";
		
		$sql = "ROLLBACK;";
		$result = mysql_query($sql);
		
			}else{
		
			$sql = "COMMIT;";
			$result = mysql_query($sql);
		
			$autoforward = 3;
			echo '<p class="right">' . $lang['success_create_topic'] . '</p>';
			echo '<p><meta http-equiv="refresh" content="' . $autoforward . '; URL=topic.php?id='. $topicid . '">' .
			'<small>(' . $lang['redirected_in'] . ' ' . $autoforward . ' ' . $lang['seconds'] . '...)</small></p><br></td>';
			echo "<script type=\"text/javascript\">showSuccessMessage();</script>";
			}
		
		}}}}}}}
		
		echo '</tr>';
		echo '</table>';
		
		?>
 
Zuletzt bearbeitet:
Zurück