Discuz is recommended! 5. PHP code highlighting and Running code implementation

Source: Internet
Author: User

Discuz! 5.0 Forum display style php code highlighting plug-in,
Make the php code syntax coloring. comrades who like PHP can easily read the code.
Added the html code running function.
This plug-in was released by a previous Comrade Discuz! 4. the php code is highlighted and the modification is complete.
In addition, my php learning has been stuck,
Hope someone can help me out of the Learning Difficulties of php
My QQ: 5642382. My QQ group: 2577162

Function: when posting a post, use the [php] php code [/php] Mark to highlight the php code,
Use the [runcode] Html code [/runcode] Mark to run HTML code (running non-server on the client ).

To add a plug-in, follow these steps:
1. Modify include/common. js
SearchCopy codeThe Code is as follows: function copycode (obj ){
Var rng = document. body. createTextRange ();
Rng. moveToElementText (obj );
Rng. scrollIntoView ();
Rng. select ();
Rng.exe cCommand ("Copy ");
Rng. collapse (false );
}

Add the following at the end of the function:Copy codeThe Code is as follows: function runCode (obj ){
Var winname = window. open (''," _ blank ",'');
Winname.doc ument. open ('text/html ', 'replace ');
Winname.doc ument. writeln (obj. value );
Winname.doc ument. close ();
}

Function saveCode (obj ){
Var winname = window. open ('', '_ blank', 'top = 10000 ');
Winname.doc ument. open ('text/html ', 'replace ');
Winname.doc ument. writeln (obj. value );
Winname.document.execcommand('saveas', '', 'code.htm ');
Winname. close ();
}

2. Modify include/discuzcode. func. php
SearchCopy codeThe Code is as follows: $ discuzcodes ['codecount'] ++;
Return "[\ tDISCUZ_CODE _ $ discuzcodes [pcodecount] \ t]";
}

(Note: You can also use Editplus to directly find the 110th lines of code to add the following code)
Continue to add below:Copy codeThe Code is as follows: // ------- Add the [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> <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 \ "> ffcod = delpost. runcode $ discuzcodes [codecount]. value; ffcod = ffcod. replace (/<br \/>/g, ''); delpost. runcode $ discuzcodes [codecount]. value = ffcod; </script> prompt: you can modify some code before running </div> <br> ";
$ Discuzcodes ['codecs'] ++;
Return "[\ tDISCUZ_CODE _ $ discuzcodes [pcodecount] \ t]";
}

// ------- Add [php] php code [/php] to highlight the php code and perform 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 PHP code] </a> </div> the PHP code is as follows: </div> <div class = \ "msgborder \" id = \ "phpcode $ discuzcodes [codecount] \"> $ code </div> <br> ";
$ Discuzcodes ['codecs'] ++;
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
SearchCopy codeThe code is as follows: $ message = preg_replace ("/\ s * \ [code \] (. + ?) \ [\/Code \] \ s */ies "," codedisp ('\ 1') ", $ message );

Add the following content:Copy codeThe Code is 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.