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", "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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to