The last font tag (RegEx, maybe?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

The last font tag (RegEx, maybe?)

Post by Gleeb »

I'm wondering how on earth I would find the *last* <font> tag that doesn't have a </font> after it is... I can't seem to find a way to do it in Regex... :| lil' help?
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

array_pop :)
Here's the code I came up with in the end. It highlights a file, plus line numbers, and retains colour across lines. Hurrah!

Code: Select all

<?PHP
$file = "source.php"; // Default page is always 1

if (isset($_GET['file']))
   $file = $_GET['file']; // If the page number was requested via GET, use that


$php_self_array = explode("/",$_SERVER['PHP_SELF']);
  // Directory Hack: http://127.0.0.1/filename.ext/arg becomes a valid and useful URL, with 'arg' being used for the page number

if (isset($php_self_array[2]) && $php_self_array[2]!="")
   $file = $php_self_array[2]; // use the page number gathered from above
?>
<style>
<!--

.hahaha {font-family: Courier, 'Courier New', sans-serif; font-size: 11px;}
.lnum {font-family: Courier, 'Courier New', sans-serif; font-size: 11px; font-weight: 700; text-align:right;}

//--!>
</style>
<table>
<?php
$contents = highlight_file($file, true);
$contents = explode("<br />", $contents);
$ln = 1;

$g=array(array('',''),array('',''));

foreach($contents as $line) {
//   array_reverse($g);
//   print_r($g);
   $line = '<font color="'.array_pop($g[1]).'">'.$line;
   print '<tr><td class="lnum">'.$ln++.']</td><td class="hahaha">'.$line.'</td>';
   preg_match_all('<font color="(#[0123456789AaBbCcDdEeFf]*)">',$line,$g);
}
?>
</table>
Post Reply