Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/06/2011, 13:22
Avatar de repara2
repara2
 
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Aportito: función para formatear código

Hola foreros, les dejo un pequeńísimo aporte; una función que recibe como parámetros el nombre del archivo y las lineas a mostrar y devuelve el código fuente formateado del archivo.
Modificar a gusto del consumidor, saludos

Código PHP:
Ver original
  1. <?
  2. /**
  3.  * Muestra código con formato HTML
  4.  * @param string $file nombre de arhivo
  5.  * @param int $line Linea a mostrar
  6.  * @param int $prev lineas antes de la linea principal
  7.  * @param int $next lineas posteriores a la linea a mostrar
  8.  * @return string
  9.  * @author repara2 adaptado de http://www.phpclasses.org/browse/file/6194.html
  10.  */
  11. function showSource($file, $line, $prev = 10, $next = 10) {
  12.    
  13.     $output = "";
  14.     //Compruebo que el archivo exista
  15.     if (!(file_exists($file) && is_file($file))) return false;
  16.  
  17.     //Leer el código
  18.     ob_start();
  19.     highlight_file($file);
  20.     $data = ob_get_contents();
  21.     ob_end_clean();
  22.  
  23.     //Separar las lineas
  24.     $data  = explode('<br />', $data);
  25.     $count = count($data) - 1;
  26.  
  27.     //Contar las lineas a desplegar
  28.     $start = $line - $prev;
  29.     if ($start < 1) {
  30.         $start = 1;
  31.     }
  32.     $end = $line + $next;
  33.     if ($end > $count) {
  34.         $end = $count + 1;
  35.     }
  36.  
  37.     //Obtener la información de formato
  38.     $highlight_default = ini_get('highlight.default');
  39.  
  40.     //Salida
  41.     $output .= '<table cellspacing="0" cellpadding="0"><tr>';
  42.     $output .= '<td style="vertical-align: top;"><code style="background-color: #FFFFCC; color: #666666;">';
  43.  
  44.     for ($x = $start; $x <= $end; $x++) {
  45.         $output .= '<a name="'.$x.'"></a>';
  46.         $output .= ($line == $x ? '<font style="background-color: red; color: white;">' : '');
  47.         $output .= str_repeat('&nbsp;', (strlen($end) - strlen($x)) + 1);
  48.         $output .= $x;
  49.         $output .= '&nbsp;';
  50.         $output .= ($line == $x ? '</font>' : '');
  51.         $output .= '<br />';
  52.     }
  53.     $output .= '</code></td><td style="vertical-align: top;"><code>';
  54.     while ($start <= $end) {
  55.         $output .= '&nbsp;' . $data[$start - 1] . '<br />';
  56.         ++$start;
  57.     }
  58.     $output .= '</code></td>';
  59.     $output .= '</tr></table>';
  60.    
  61.     return $output;
  62.  
  63. }
  64.  
  65. //<----------- Ejemplo
  66. //El archivo que quiero ver
  67. $file = basename($_SERVER['PHP_SELF']);
  68. //Las lineas que quiero mostrar
  69. $line = 10;
  70. //Obtener el HTML
  71. if(!$output = showSource($file, $line))die("Archivo inexistente!");
  72. //Salida por pantalla
  73. echo "<h3>Mostrando $line lineas del archivo $file</h3>";
  74. echo $output;
  75. ?>
__________________
Fere libenter homines, id quod volunt, credunt.