Hi,

Friday, April 4, 2003, 3:57:25 PM, you wrote:
TR> Hi,

TR> 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

TR> This line will always cause your function to return 2

TR>         static $line = 1;

TR> One solution is to use a second parameter to reset the counter like this:

TR> function addPrefix( $matches, $reset=False )
TR> {
TR>     static $line;
TR>     if(!$reset) return $line++ . $matches[0];
TR>     else $line = 0;
TR> }

TR> $url = split("\?", $_GET['url']);
TR> //reset counter
TR> addPrefix('',True);
TR> $source = highlight_file($url[0], true);


TR> The regex I'll leave to others :)
TR> -- 
TR> regards,
TR> Tom

Ignore last email I was asleep :)
static $line = 1; works just fine

-- 
regards,
Tom


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

Reply via email to