note 127202 added to function.highlight-file
| From: | cehem at main dot php dot net | Date: | Tue, 28 Jun 2022 17:45:39 +0000 |
| Subject: | note 127202 added to function.highlight-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-218659@lists.php.net to get a copy of this message | ||
This little code returns the file formatted as an array, line by line. You are free to choose which
lines to display, and/or to add the line numbers in front of them. Index 0 = Line 1
<?php
function highlightArray($file) {
$cssRegexp = '/\<span style=\"([^\"]+)\"\>/';
# formatte le fichier complet
$highlitedFile = highlight_file($file, true);
# retire le tag <code> et <span> général
$code = substr($highlitedFile, 36, -15);
# récupère le css du <span> général
$globCss = preg_match($cssRegexp, substr($highlitedFile, 0, 36), $matche);
$globCss = $matche[1];
# découpe le fichier en array
$array = explode('<br />', $code);
$lastCss = null;
$lines = array_map(function($line) use (&$lastCss, $globCss) {
# nettoie les lignes vides
if(preg_match('/^( )*$/',$line)) $line = ' ';
# ajoute le dernier style ouvert s'il est stocké
if(!empty($lastCss)) $line = '<span style="'.$lastCss
.'">'. $line;
# compare les ouverture et fermetures de span
$diff = substr_count($line, '<span') - substr_count($line,
'</span');
# s'il y a une différence, ferme tous les span et enregistre le dernier style
if($diff > 0) {
$line .= str_repeat('</span>', $diff);
$lastCss = preg_match_all('/\<span
style=\"([^\"]+)\"\>/', $line, $matches);
$lastCss = array_pop($matches[1]);
} else {
$lastCss = NULL;
}
# restaure le css général sur chaque ligne
return "<span style=\"$globCss\">$line</span>";
}, $array);
return $lines;
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.133 (proxied: 2a01:e0a:2a1:6690:a5b7:e10b:912c:912a)
----
Manual Page -- https://php.net/manual/en/function.highlight-file.php
Edit -- https://main.php.net/note/edit/127202
Del: integrated -- https://main.php.net/note/delete/127202/integrated
Del: useless -- https://main.php.net/note/delete/127202/useless
Del: bad code -- https://main.php.net/note/delete/127202/bad+code
Del: spam -- https://main.php.net/note/delete/127202/spam
Del: non-english -- https://main.php.net/note/delete/127202/non-english
Del: in docs -- https://main.php.net/note/delete/127202/in+docs
Del: other reasons-- https://main.php.net/note/delete/127202
Reject -- https://main.php.net/note/reject/127202
Search -- https://main.php.net/manage/user-notes.php