Page 6 of 8

Posted: Mon Apr 16, 2007 10:42 pm
by Kieran Huggins
it's all in good fun zg3k, we're still here to help

Posted: Tue Apr 17, 2007 3:53 pm
by ziggy3000
ok. i think i got the preg_replace_callback function right...
this is my code now

Code: Select all

<html>
<head>
<title>Parser Test</title>
<style type="text/css">
/* Form elements */
input,textarea, select {
	color : #000000;
	font: normal 11px Verdana, Arial, Helvetica, sans-serif;
	border-color : #000000;
}

/* The text input fields background colour */
input.post, textarea.post, select {
	background-color : #FFFFFF;
}

input { text-indent : 2px; }

/* The buttons used for bbCode styling in message post */
input.button {
	background-color : #EFEFEF;
	color : #000000;
	font-size: 11px; font-family: Verdana, Arial, Helvetica, sans-serif;
}

/* The buttons used for bbCode styling in message post */
button {
	background-color : #EFEFEF;
	color : #000000;
	font-size: 11px; font-family: Verdana, Arial, Helvetica, sans-serif;
}

/* The main submit button option */
input.mainoption {
	background-color : #FAFAFA;
	font-weight : bold;
}

/* None-bold submit button */
input.liteoption {
	background-color : #FAFAFA;
	font-weight : normal;
}

/* This is the line in the posting page which shows the rollover
  help line. This is actually a text box, but if set to be the same
  colour as the background no one will know 
*/
.helpline { background-color: #FFF; border-style: none; }

</style>
<script type="text/javascript">

// Helpline messages
b_help = "Bold text: [b]text[/b]  (alt+b)";
i_help = "Italic text: [i]text[/i]  (alt+i)";
u_help = "Underline text: [u]text[/u]  (alt+u)";
q_help = "Quote text: [quote]text[/quote]  (alt+q)";
c_help = "Code display:

Code: Select all

code
(alt+c)";
l_help = "List:
  • text
(alt+l)";
o_help = "Ordered list:
  • text
(alt+o)";
p_help = "Insert image: [img]http://image_url[/img] (alt+p)";
w_help = "Insert URL: http://url or URL text (alt+w)";
a_help = "Close all open bbCode tags";
s_help = "Font color: text Tip: you can also use color=#FF0000";
f_help = "Font size: [size=x-small]small text[/size]";
strike_help = "Strike-through text: [s]text[/s] (alt+t)";
PHP_help = "Highlight the PHP syntax.

Code: Select all

php code
(Alt+d)";

// Shows the help messages in the helpline window
function helpline(help) {
document.post.helpbox.value = eval(help + "_help");
}



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 = "

Code: Select all

<?php
echo 'This Works!';
?> 

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

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 />';
}

function bbcode($strings)
{
$search = array(
'#\[b\](.+?)\[\/b\]#is',
'#\[i\](.+?)\[\/i\]#is',
'#\[u\](.+?)\[\/u\]#is',
'#\[img\](.+?)\[\/img\]#is',
'#\(.+?)\[\/url\]#is',
'#\[color=(.+?)\](.+?)\[\/color\]#is',
'#\

Code: Select all

(.+?)\[\/code\]#is',
    );
    $replace = array( 
        '<b>$1</b>',
        '<i>$1</i>',
        '<u>$1</u>',
        '<img src="$1">',
        '<a href="$1">$2</a>',
        '<font color="$1">$2</font>',
        '<div class="code" id="$1">$2</div>',
    );
    return preg_replace($search, $replace, $strings);

}
echo preg_replace_callback('#\[php\](.+?)\[\/php\]#is', "hc", $string);


$string2 = bbcode($string);
echo $string2;
}
?>
<fieldset>
	<button onclick="addPTag(document.getElementById('text'),'b')" onmouseover="helpline('b')"><b>Bold</b></button>
	<button onclick="addPTag(document.getElementById('text'),'i')" onmouseover="helpline('i')"><i>Italic</i></button>
	<button onclick="addPTag(document.getElementById('text'),'u')" onmouseover="helpline('u')"><u>Underline</u></button>
	<button onclick="addPTag(document.getElementById('text'),'url')" onmouseover="helpline('w')"><u><a href="#">Url</a></u></button>
	<button onclick="addPTag(document.getElementById('text'),'img')" onmouseover="helpline('p')">Image</button>
	<button onclick="addPTag(document.getElementById('text'),'php')" onmouseover="helpline('php')">Php</button>
	<br>
	<tr>
	<input name="helpbox" size="45" maxlength="100" style="width: 450px; font-size: 10px;" class="helpline" value="Tip: Styles can be applied quickly to selected text." type="text">
	</tr>
	<br> 
	<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 problem now is that when i type in [url=test.php][ i][ u][ b]hi[/b][/u][/i][/url] [ php] php code  it shows 1 highlighted line
"1.   Array" (not in quotes) and in the next line i see
"[url=test.php][ i][ u][ b][b][i][u]hi[/b][/i][/u][/b][/u][/i][/url]hi[ php]php code "

since it is highlighting something, i know i got the preg_replace_callback function right. but it repeats what i typed in the textarea, then parses it, and it doesn't highlight the string :evil: , how should i change the code so that it highlights between php tags and and parses other stuff, without repeating the unparsed bbcode?

Posted: Tue Apr 17, 2007 4:11 pm
by John Cartwright
I think your going to have a tough time asking people to look through all that code and try and debug it for you.

My advise, strip it down the base essentials and debug from there to find out where it is screwing up.

Also, I don't think you quite understand how to use preg_replace_callback() yet. Your hc function should only be expecting one parameter (an array of matches).

Posted: Tue Apr 17, 2007 4:22 pm
by ziggy3000
can you guys please tell me how to fix the preg_replace_callback() function? i will learn from my mistake, and maybe i'll be able to fix my code. i fixed part of it just now. so now it doesn't repeat the unparsed bbcode. :D

Posted: Tue Apr 17, 2007 4:30 pm
by John Cartwright
Like I said, that's a lot to ask, and I know I certainly don't have the time or desire to debug your code for you.

I've already told you why your preg_replace_callback() function isn't working..

Posted: Tue Apr 17, 2007 4:36 pm
by ziggy3000
i'm am not telling you to debug all my code, or any code. i am asking you to give me 1 line of code
all i need

Code: Select all

echo preg_replace_callback("#\[php\](.+?)\[\/php\]#is",  other stuff);
and yes... you did tell me why it isn't working, but you didn't tell me how to fix it.(I don't know :roll: )

Posted: Tue Apr 17, 2007 4:37 pm
by RobertGonzalez
Jcart wrote:Your hc function should only be expecting one parameter (an array of matches).
This should help you tremendously. Instead of the three params the function is expecting now, it should be one, of type array, that holds the matches from the preg_ function. That little piece of information will, in and of itself, alleviate most of your issues with using the callback.

Posted: Tue Apr 17, 2007 4:40 pm
by ziggy3000
but i didn't code the hc() function, kai sellgren did...
so i dont know how to change the hc function... i might screw it up so it doesn't highlight at all

Posted: Tue Apr 17, 2007 5:04 pm
by RobertGonzalez
That is an excellent way to learn though. Besides, there is always the idea that you could copy files and roll back to what worked if something to goes to hell in a handbasket.

Take a chance and see what happens. They are only files. The worst thing that could happen is you break the entire app and have to undo something you did in development (by copying the working files right over them). Good luck.

Posted: Tue Apr 17, 2007 5:09 pm
by John Cartwright
We're not going to hold your hand through every line of code, sorry, that's not why were here..

Try modifying your code and play around with it. If you get stuck on something specific then we'll keep nudging you.

Posted: Tue Apr 17, 2007 6:05 pm
by ziggy3000
ok i removed the parameters in the function

can you just take a quick look if the change i made was right. i dont need you to debug it, because i have a debugger, and it didn't sense any errors.

Original:

Code: Select all

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 />';
 }

Modified:

Code: Select all

function hc($code)
 {
  $code_h = highlight_string($code,true);
  $code_h = preg_replace("/<\\?php.*?<br \\/>/","",$code_h);
  $code_h_lines = explode("<br />",$code_h);
  $total_lines = count($code_h_lines);
  $name = "PHP Code";
  $name = "<strong>$name</strong><br /><br />";
  echo '<br />'.$name.'<div class="phpcode"><table border="0" style="border-collapse: collapse" cellpadding="4" cellspacing="0">
<tr>
';
  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 />';
}

Posted: Tue Apr 17, 2007 6:15 pm
by RobertGonzalez
Everah wrote:
Jcart wrote:Your hc function should only be expecting one parameter (an array of matches).
This should help you tremendously. Instead of the three params the function is expecting now, it should be one, of type array, that holds the matches from the preg_ function. That little piece of information will, in and of itself, alleviate most of your issues with using the callback.
Is $code an array of matches?

Posted: Tue Apr 17, 2007 6:20 pm
by ziggy3000
no thats when you use bbcode() (short code that replaces tags with html code)
$code is supossed to be used like a normal variable

Code: Select all

<?php
function hc()
{
//code
}
$code = '<?php phpinfo(); ?>';
hc($code); // using my modified version of hc
hc($code,1,1); // using  original version of hc

?>

Posted: Tue Apr 17, 2007 6:36 pm
by RobertGonzalez
If you are going to use this, you are basically saying take all matches to the pattern, read them into $string and run them through the function hc().

Code: Select all

echo preg_replace_callback('#\[php\](.+?)\[\/php\]#is', "hc", $string);

Posted: Tue Apr 17, 2007 7:16 pm
by aaronhall
I think this thread's about due for an updated summary, Kieran ;)