highlight_file example


    private function fileExcerpt(string $file, int $line, int $srcContext = 3): string
    {
        if (is_file($file) && is_readable($file)) {
            // highlight_file could throw warnings             // see https://bugs.php.net/25725             $code = @highlight_file($file, true);
            if (\PHP_VERSION_ID >= 80300) {
                // remove main pre/code tags                 $code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
                // split multiline code tags                 $code = preg_replace_callback('#<code ([^>]++)>((?:[^<]*+\\n)++[^<]*+)</code>#', fn ($m) => "<code $m[1]>".str_replace("\n", "</code>\n<code $m[1]>", $m[2]).'</code>', $code);
                // Convert spaces to html entities to preserve indentation when rendered                 $code = str_replace(' ', '&nbsp;', $code);
                $content = explode("\n", $code);
            } else {
                // remove main code/span tags                 $code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
                
return strip_tags($this->formatArgs($args));
    }

    /** * Returns an excerpt of a code file around the given line number. */
    public function fileExcerpt(string $file, int $line, int $srcContext = 3): ?string
    {
        if (is_file($file) && is_readable($file)) {
            // highlight_file could throw warnings             // see https://bugs.php.net/25725             $code = @highlight_file($file, true);
            if (\PHP_VERSION_ID >= 80300) {
                // remove main pre/code tags                 $code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
                // split multiline code tags                 $code = preg_replace_callback('#<code ([^>]++)>((?:[^<]*+\\n)++[^<]*+)</code>#', fn ($m) => "<code $m[1]>".str_replace("\n", "</code>\n<code $m[1]>", $m[2]).'</code>', $code);
                // Convert spaces to html entities to preserve indentation when rendered                 $code = str_replace(' ', '&nbsp;', $code);
                $content = explode("\n", $code);
            } else {
                // remove main code/span tags                 $code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
                

    private function fileExcerpt(string $file, int $line, int $srcContext = 3): string
    {
        if (is_file($file) && is_readable($file)) {
            // highlight_file could throw warnings             // see https://bugs.php.net/25725             $code = @highlight_file($file, true);
            // remove main code/span tags             $code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
            // split multiline spans             $code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', fn ($m) => "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>', $code);
            $content = explode('<br />', $code);

            $lines = [];
            if (0 > $srcContext) {
                $srcContext = \count($content);
            }

            
Home | Imprint | This part of the site doesn't use cookies.