[solved] php highlight function help
Moderator: General Moderators
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
ok. i think i got the preg_replace_callback function right...
this is my code now (alt+c)";
l_help = "List:
o_help = "Ordered list:
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. (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 = "
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',
'#\
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
codel_help = "List:
- text
o_help = "Ordered list:
- text
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// 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?- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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).
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).
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
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
)
all i need
Code: Select all
echo preg_replace_callback("#\[php\](.+?)\[\/php\]#is", other stuff);
Last edited by ziggy3000 on Tue Apr 17, 2007 4:37 pm, edited 1 time in total.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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).
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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:
Modified:
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 />';
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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).
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 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
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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);