Posted: Mon Apr 16, 2007 10:42 pm
it's all in good fun zg3k, we're still here to help
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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
codeCode: Select all
php codeCode: Select all
<?php
echo 'This Works!';
?> 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?Code: Select all
echo preg_replace_callback("#\[php\](.+?)\[\/php\]#is", other stuff);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.Jcart wrote:Your hc function should only be expecting one parameter (an array of matches).
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 />';
}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 />';
}Is $code an array of matches?Everah wrote: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.Jcart wrote:Your hc function should only be expecting one parameter (an array of matches).
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
?>Code: Select all
echo preg_replace_callback('#\[php\](.+?)\[\/php\]#is', "hc", $string);