Re: Q. Adding line numbers to highlighted source
| From: | Tom Rogers | Date: | Fri, 04 Apr 2003 05:57:25 +0000 |
| Subject: | Re: Q. Adding line numbers to highlighted source | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-142259@lists.php.net to get a copy of this message | ||
Hi,
Friday, April 4, 2003, 3:27:55 PM, you wrote:
AM> I've been trying to add line-numbers to my source code, but can't seem to
AM> work out how to do it.
AM> As an example, I tried the following:
AM> <?php
AM> function addPrefix( $matches )
AM> {
AM> static $line = 1;
AM> return $line++ . $matches[0];
AM> }
AM> $url = split("\?", $_GET['url']);
AM> $source = highlight_file($url[0], true);
AM> $source = preg_replace_callback("
\<br \/\>U",
AM> "addPrefix", $source);
AM> echo $source;
?>>
AM> which is called whenever the use user on a link like <a
AM> href="show-source.php?url=index.php">Show Source</a>
AM> The problem is that my regex does not correcly match the lines.
AM> Anyone got any bright ideas?
AM> --
AM> Alan McFarlane
This line will always cause your function to return 2
static $line = 1;
One solution is to use a second parameter to reset the counter like this:
function addPrefix( $matches, $reset=False )
{
static $line;
if(!$reset) return $line++ . $matches[0];
else $line = 0;
}
$url = split("\?", $_GET['url']);
//reset counter
addPrefix('',True);
$source = highlight_file($url[0], true);
The regex I'll leave to others :)
--
regards,
Tom