Recommended discuz!5 PHP code highlighting and implementation of the code can be run _php skills

Source: Internet
Author: User
Tags php code
discuz!5.0 Forum display style PHP code highlighting plugin,
The PHP code syntax coloring, like a comrade PHP can easily see the code.
Also added the HTML code run function.
This plugin was modified on the basis of a previous comrade's discuz!4 PHP code highlighting.
In addition, my PHP learning into a stagnant state,
I hope someone can help me out of the learning predicament of PHP
My qq:5642382 QQ group: 2577162

Function: When posting, use [php]php code [/php] tag to highlight the PHP code,
Use the [runcode]html code [/runcode] tag to run the HTML code (non-server running on the client).

Plug-in add steps as follows:
1. Modify Include/common.js
Find
Copy Code code as follows:

function Copycode (obj) {
var rng = Document.body.createTextRange ();
Rng.movetoelementtext (obj);
Rng.scrollintoview ();
Rng.select ();
Rng.execcommand ("Copy");
Rng.collapse (FALSE);
}

At the end of this function, add the following:
Copy Code code as follows:

function RunCode (obj) {
var winname = window.open ("," _blank ",");
Winname.document.open (' text/html ', ' replace ');
Winname.document.writeln (Obj.value);
Winname.document.close ();
}

function Savecode (obj) {
var winname = window.open (', ' _blank ', ' top=10000 ');
Winname.document.open (' text/html ', ' replace ');
Winname.document.writeln (Obj.value);
Winname.document.execCommand (' SaveAs ', ', ', ' code.htm ');
Winname.close ();
}

2. Modify include/discuzcode.func.php
Find
Copy Code code as follows:

$discuzcodes [' Codecount ']++;
Return "[\tdiscuz_code_$discuzcodes[pcodecount]\t]";
}

(Note: You can also use EditPlus directly to find the 110th line of code to do the following add)
Continue to add below:
Copy Code code as follows:

-------Add [RunCode] HTML code [/runcode] to run the HTML code
function Runcodedisp ($code) {
Global $discuzcodes;
$discuzcodes [' Pcodecount ']++;
$code = Htmlspecialchars (str_replace (' \ "', '" '), Preg_replace ("/^[\n\r]*" (. +?) [\n\r]*$/is "," \\1 ", $code)));
         $discuzcodes [' codehtml '] [$discuzcodes [' Pcodecount ']] =   "<br><br><div class=\" smalltxt\ " style=\" margin-left: 2em;  Margin-right: 2em\ "><textarea name=\" runcode$discuzcodes[codecount]\ " rows=\" 1\ "  Cols=\ "95\"  style=\ "height:200px\" > $code </textarea><br><input type=\ "button\"  value=\ "Run code \  onclick=\" RunCode (runcode$discuzcodes[codecount) \ "> <input type=\" button\ " value=\" copy code \ " onclick=\" Copycode (Runcode$discuzcodes[codecount]) \ "> <input  type=\ "button\"  value=\ "Save code \"  onclick=\ "Savecode (Runcode$discuzcodes[codecount]) \" >  <script language=\ "Javascript\" &GT;FFCOD&NBSP;=&NBSP;DELPOST.RUNCODE$DISCUZCODES[CODECOUNT]&NBSP; Value; ffcod = ffcod.replace (/<br \/>/g, ');  delpost.runcode$discuzcodes[ Codecount] .value = ffcod;</script>  Tip: You can modify some of the code before running </div><br>; 
$discuzcodes [' Codecount ']++;
Return "[\tdiscuz_code_$discuzcodes[pcodecount]\t]";
}

-------Add [PHP] PHP code [/php] to highlight the PHP code, for PHP syntax coloring
function Phpcodedisp ($code) {
Global $discuzcodes;
$discuzcodes [' Pcodecount ']++;
$code = Phphighlite (Str_replace ("\\\", "\" ", $code));
$discuzcodes [' codehtml '] [$discuzcodes [' pcodecount ']] = "<br><div class=\" msgheader\ "><div class=\" Right\ "><a href=\" ###\ "class=\" smalltxt\ "Onclick=\" Copycode ($ (' phpcode$discuzcodes[codecount] '); \ ">[ Copy the PHP code]</a></div>php code as follows: </div><div class=\ "Msgborder\ id=\" Phpcode$discuzcodes[codecount ]\ "> $code </div><br>";
$discuzcodes [' Codecount ']++;
Return "[\tdiscuz_code_$discuzcodes[pcodecount]\t]";
}

function Phphighlite ($code) {
if (!strpos ($code, "<?\n") &&!strpos ($code, ';? ') && substr ($code, 0,2)!= ' {
$code = ' < '. Trim ($code). ' > ';
$addedtags = 1;
}
Ob_start ();
$oldlevel = error_reporting (0);
Highlight_string ($code);
Error_reporting ($oldlevel);
$buffer = Ob_get_contents ();
Ob_end_clean ();
if ($addedtags) {
$openingpos = Strpos ($buffer, ';? ');
$closingpos = Strrpos ($buffer, '? ');
$buffer = substr ($buffer, 0, $openingpos). substr ($buffer, $openingpos +5, $closingpos-($openingpos +5)). substr ($buffer , $closingpos +5);
}
$buffer = Str_replace (' "," \ "), $buffer);
$buffer = Str_replace (' <br/> ', ', $buffer);
return $buffer;
}
-------End

3. Continue to modify include/discuzcode.func.php
Find
Copy Code code as follows:

$message = Preg_replace ("/\s*\[code\]" (. +?) \[\/code\]\s*/ies "," Codedisp (' \\1 ') ", $message);

Continue to add below:
Copy Code code as follows:

-------RunCode
$message = Preg_replace ("/\s*\[runcode\]" (. +?) \[\/runcode\]\s*/ies "," Runcodedisp (' \\1 ') ", $message);
-------PHP
$message = Preg_replace ("/\s*\[php\]" (. +?) \[\/php\]\s*/ies "," Phpcodedisp (' \\1 ') ", $message);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.