Jump to content

Highlight String Function


Flukey

Recommended Posts

Hey guys,

This code was found on php.net/highlight_string under the comments section.

Anyhows, there is a slight flaw with it, being that it can't parse multi-line comments as an error is thrown up.

[code=php:0]

// Function used to highlight code and print line numbers
function printCode($code, $high_light, $lines_number)
  {
            if (!is_array($code)) $code = explode("\n", $code);
                $count_lines = count($code);
                    foreach ($code as $line => $code_line) {
                        if ($lines_number) $r1 = "<span class=\"lines_number\">".($line + 1)." </span>";
                            if ($high_light) {
                                if (ereg("<\?(php)?[^[:graph:]]", $code_line))
                                {
                                    $r2 = highlight_string($code_line, 1)."<br />";
                                }
                                else
                                {
                                    $r2 = ereg_replace("(&lt;\?php&nbsp;)+", "", highlight_string("<?php ".$code_line, 1))."<br />";
                                }
                            }
                            else
                            {
                              $r2 = (!$line) ? "<pre>" : "";
                              $r2 .= htmlentities($code_line);
                              $r2 .= ($line == ($count_lines - 1)) ? "<br /></pre>" : "";
                            }
                            echo "<table>";
                            $r .= "<tr><td>$r1</td><td>$r2</td></tr>";
                    }
                    echo "<div class=\"code\">".$r."</div>";
  }
[/code]

Any ideas how to modify that to accept multi-line comments?

I understand how the function works, but i'm not sure how to modify to get it to do what I want.

Cheers guys,

Flukes :)
Link to comment
https://forums.phpfreaks.com/topic/29090-highlight-string-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.