Page 1 of 1

bbcode help

Posted: Mon Apr 16, 2007 1:37 pm
by ziggy3000
this code is from what everah gave me, and i modified it

Code: Select all

<html>
<head>
<title>Parser Test</title>
<script type="text/javascript">
function getSelection(ta) { 
	var bits = [ta.value,'','','']; 
	if (document.selection) { 
		var vs = '#$%^%$#';
		var tr = document.selection.createRange();
		if (tr.parentElement() != ta) {
			return null;
		}
		
		bits[2] = tr.text;
		tr.text = vs;
		fb = ta.value.split(vs);
		tr.moveStart('character', -vs.length);
		tr.text = bits[2];
		bits[1] = fb[0];
		bits[3] = fb[1];
	} else { 
		if (ta.selectionStart == ta.selectionEnd) {
			return null;
		}
		
		bits = (new RegExp('([\x00-\xff]{'+ta.selectionStart+'})([\x00-\xff]{'+(ta.selectionEnd - ta.selectionStart)+'})([\x00-\xff]*)')).exec(ta.value);
	}
	return bits;
}

function matchPTags(str) { 
	str = ' ' + str + ' ';
	ot = str.split(/\[[B|U|I|URL|IMG|PHP].*?\]/i);
	ct = str.split(/\[\/[B|U|I|URL|IMG|PHP].*?\]/i);
	return ot.length == ct.length;
}

function addPTag(ta, pTag) { 
	bits = getSelection(ta);
	if(bits) { 
		if (!matchPTags(bits[2])) { 
			alert('\t\tInvalid Selection\nSelection contains unmatched opening or closing tags.');
			return;
		}
		
		ta.value = bits[1] + '[' + pTag + ']' + bits[2] + '[/' + pTag + ']' + bits[3];
	}
}
</script>
</head>
<body>
<?php

$string = "[php]<?php
echo 'This Works!';
?> [/php]
 
[b]hello[/b]";
if (isset($_POST['string'])) {
$string = isset($_POST['string']) ? $_POST['string'] : $string;
echo '<h3>Preview</h3>';

function bbcode($strings)
{
$search = array( 
        '#\[b\](.+?)\[\/b\]#is',
        '#\[i\](.+?)\[\/i\]#is',
        '#\[u\](.+?)\[\/u\]#is',
        '#\[img\](.+?)\[\/img\]#is',
        '#\[url=(.+?)\](.+?)\[\/url\]#is',
        '#\[color=(.+?)\](.+?)\[\/color\]#is',
        '#\[php\](.+?)\#\[\/php\]#is',
    );
    $replace = array( 
        '<b>$1</b>',
        '<i>$1</i>',
        '<span style="border-bottom: 1px solid;">$1</span>',
        '<img src="$1">',
        '<a href="$1">$2</a>',
        '<font color="$1">$2</font>',
        '<div class="php"><?php $code3 = "$1"; hc($code,1,1); ?></div>',
    );
    return preg_replace($search, $replace, $strings);
}


function hc($code,$ln = 1,$phptags = 1,$name = "")
 {
  $code_h = highlight_string($code,true);
  if (!$phptags)
   $code_h = preg_replace("/<\\?php.*?<br \\/>/","",$code_h);
  $code_h_lines = explode("<br />",$code_h);
  $total_lines = count($code_h_lines);
  if ($name !== "")
   $name = "<strong>$name</strong><br /><br />";
  echo '<br />'.$name.'<div class="phpcode"><table border="0" style="border-collapse: collapse" cellpadding="4" cellspacing="0">
<tr>
';
  if ($ln)
   {
    echo '<td>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><code style="color: #e28000;">';
    for ($a = 0;$a < $total_lines;$a++)
     echo ($a+1)."<br />";
    echo '</div></td>
</tr>
</table>
';
    echo '</td>
';
   }
  echo '<td>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>

';
  for ($a = 0;$a < $total_lines;$a++)
   echo ($code_h_lines[$a])."<br />";
  echo '

</td>
</tr>
</table>
';
  echo '</td>
</tr>
</table></div><br />';
 }
 
 
$string = bbcode($string);
echo $string;
}
?>
<fieldset>
	<button onclick="addPTag(document.getElementById('text'),'b')"><b>Bold</b></button>
	<button onclick="addPTag(document.getElementById('text'),'i')"><i>Italic</i></button>
	<button onclick="addPTag(document.getElementById('text'),'u')"><u>Underline</u></button>
	<button onclick="addPTag(document.getElementById('text'),'url')"><u><a href="#">Url</a></u></button>
	<button onclick="addPTag(document.getElementById('text'),'img')">Image</button>
	<button onclick="addPTag(document.getElementById('text'),'php')">Php</button>
	<form action="<?php echo basename(__FILE__); ?>" method="post">
	<textarea id="text" name="string" cols="150" rows="10"><?php echo $string; ?></textarea></p>
	<input type="submit" name="Test" value="Preview" >
	<input type="submit" name="post" value="Post"
	</form>
</fieldset>
</body>
</html>
the php tag is supposed to highlight the code,
and the rest should... well you know what they have to do.
the problem is that it either highlights whatever i input
or it parses the bbcode tags but it doesn't highlight the php code
i want it to highlight code between the php tags, and parse other tags at the same time

the way you use my highlight function is
hc(code, show tags?, show line numbers?, name?);

does anyone know how to fix this?

the bbcode tutorial is here
http://www.tutorialninja.com/viewtopic.php?f=7&t=27

Posted: Mon Apr 16, 2007 1:43 pm
by RobertGonzalez
This topic is under discussion here --> viewtopic.php?p=374382#374382

Locked.Image