Re: [PHP] Re: Q. Adding line numbers to highlighted source
I suspect that is because of the regex code evaluation; but thanks for the suggestion anyway. BTW, I found a way to speed (my version) even more, something that I still forget to use on a regular basis but has been proven to be ~extremely~ good at boosting speed: replace: for ($i = 0; $i count($source); $i++) with: for ($i = 0, $_count = count($source); $i $_count; $i++) This simply prevents a continual reevaluation of count($source) (or whatever) and can provide some spectacular gains -- Alan John W. Holmes [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] This also works the same, for comparisons sake, but your solution benchmarks faster: $i=2; $fp = fopen($url,'r'); $file = fread($fp,filesize($url)); $file = highlight_string($file,TRUE); $file = str_replace('code','codespan style=color:black;1/span',$file); $file = preg_replace('/br \//e','sprintf(br /span style=\color:black;\%05d /span,$i++)',$file); echo hr; echo $file; Enjoy! ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -Original Message- From: Alan McFarlane [mailto:[EMAIL PROTECTED] Sent: Friday, April 04, 2003 12:46 PM To: [EMAIL PROTECTED] Subject: [PHP] Re: Q. Adding line numbers to highlighted source Well I finally found a workable solution: $url = index.php; /* for example */ $source = explode(br /, preg_replace(`\/?code\`Ui, , highlight_file($url, true))); for ($i = 0; $i count ($source); $i++) $source[$i] = span style=\color:black;\ . str_pad($i, 5, 0, STR_PAD_LEFT) . /span . $source[$i]; echo code . implode(br /, $source) . /code; Alan McFarlane [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I've been trying to add line-numbers to my source code, but can't seem to work out how to do it. As an example, I tried the following: ?php function addPrefix( $matches ) { static $line = 1; return $line++ . $matches[0]; } $url = split(\?, $_GET['url']); $source = highlight_file($url[0], true); $source = preg_replace_callback(`\br \/\`U, addPrefix, $source); echo $source; ? which is called whenever the use user on a link like a href=show-source.php?url=index.phpShow Source/a The problem is that my regex does not correcly match the lines. Anyone got any bright ideas? -- Alan McFarlane -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Q. Adding line numbers to highlighted source
FWIW: There is a proposal to add a runtime option to view line numbers in the source, something like: http://www.example.com/hi.phps?SHOW_LINENUMBERS In fact, some even thought it was applied but alas it never was. There is a little information on this feature request here: http://bugs.php.net/bug.php?id=15208 Don't hesistate to vote for this feature request :) Regards, Philip On Sat, 5 Apr 2003, Alan McFarlane wrote: I suspect that is because of the regex code evaluation; but thanks for the suggestion anyway. BTW, I found a way to speed (my version) even more, something that I still forget to use on a regular basis but has been proven to be ~extremely~ good at boosting speed: replace: for ($i = 0; $i count($source); $i++) with: for ($i = 0, $_count = count($source); $i $_count; $i++) This simply prevents a continual reevaluation of count($source) (or whatever) and can provide some spectacular gains -- Alan John W. Holmes [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] This also works the same, for comparisons sake, but your solution benchmarks faster: $i=2; $fp = fopen($url,'r'); $file = fread($fp,filesize($url)); $file = highlight_string($file,TRUE); $file = str_replace('code','codespan style=color:black;1/span',$file); $file = preg_replace('/br \//e','sprintf(br /span style=\color:black;\%05d /span,$i++)',$file); echo hr; echo $file; Enjoy! ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -Original Message- From: Alan McFarlane [mailto:[EMAIL PROTECTED] Sent: Friday, April 04, 2003 12:46 PM To: [EMAIL PROTECTED] Subject: [PHP] Re: Q. Adding line numbers to highlighted source Well I finally found a workable solution: $url = index.php; /* for example */ $source = explode(br /, preg_replace(`\/?code\`Ui, , highlight_file($url, true))); for ($i = 0; $i count ($source); $i++) $source[$i] = span style=\color:black;\ . str_pad($i, 5, 0, STR_PAD_LEFT) . /span . $source[$i]; echo code . implode(br /, $source) . /code; Alan McFarlane [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I've been trying to add line-numbers to my source code, but can't seem to work out how to do it. As an example, I tried the following: ?php function addPrefix( $matches ) { static $line = 1; return $line++ . $matches[0]; } $url = split(\?, $_GET['url']); $source = highlight_file($url[0], true); $source = preg_replace_callback(`\br \/\`U, addPrefix, $source); echo $source; ? which is called whenever the use user on a link like a href=show-source.php?url=index.phpShow Source/a The problem is that my regex does not correcly match the lines. Anyone got any bright ideas? -- Alan McFarlane -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Q. Adding line numbers to highlighted source
Hi Alan, Another solution is to use the HighlightParser that comes with phpDocumentor (http://www.phpdoc.org) Regards, Greg -- phpDocumentor http://www.phpdoc.org Alan McFarlane wrote: I've been trying to add line-numbers to my source code, but can't seem to work out how to do it. As an example, I tried the following: ?php function addPrefix( $matches ) { static $line = 1; return $line++ . $matches[0]; } $url = split(\?, $_GET['url']); $source = highlight_file($url[0], true); $source = preg_replace_callback(`\br \/\`U, addPrefix, $source); echo $source; ? which is called whenever the use user on a link like a href=show-source.php?url=index.phpShow Source/a The problem is that my regex does not correcly match the lines. Anyone got any bright ideas? -- Alan McFarlane -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Q. Adding line numbers to highlighted source
Well I finally found a workable solution: $url = index.php; /* for example */ $source = explode(br /, preg_replace(`\/?code\`Ui, , highlight_file($url, true))); for ($i = 0; $i count ($source); $i++) $source[$i] = span style=\color:black;\ . str_pad($i, 5, 0, STR_PAD_LEFT) . /span . $source[$i]; echo code . implode(br /, $source) . /code; Alan McFarlane [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I've been trying to add line-numbers to my source code, but can't seem to work out how to do it. As an example, I tried the following: ?php function addPrefix( $matches ) { static $line = 1; return $line++ . $matches[0]; } $url = split(\?, $_GET['url']); $source = highlight_file($url[0], true); $source = preg_replace_callback(`\br \/\`U, addPrefix, $source); echo $source; ? which is called whenever the use user on a link like a href=show-source.php?url=index.phpShow Source/a The problem is that my regex does not correcly match the lines. Anyone got any bright ideas? -- Alan McFarlane -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Q. Adding line numbers to highlighted source
This also works the same, for comparisons sake, but your solution benchmarks faster: $i=2; $fp = fopen($url,'r'); $file = fread($fp,filesize($url)); $file = highlight_string($file,TRUE); $file = str_replace('code','codespan style=color:black;1/span',$file); $file = preg_replace('/br \//e','sprintf(br /span style=\color:black;\%05d /span,$i++)',$file); echo hr; echo $file; Enjoy! ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -Original Message- From: Alan McFarlane [mailto:[EMAIL PROTECTED] Sent: Friday, April 04, 2003 12:46 PM To: [EMAIL PROTECTED] Subject: [PHP] Re: Q. Adding line numbers to highlighted source Well I finally found a workable solution: $url = index.php; /* for example */ $source = explode(br /, preg_replace(`\/?code\`Ui, , highlight_file($url, true))); for ($i = 0; $i count ($source); $i++) $source[$i] = span style=\color:black;\ . str_pad($i, 5, 0, STR_PAD_LEFT) . /span . $source[$i]; echo code . implode(br /, $source) . /code; Alan McFarlane [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I've been trying to add line-numbers to my source code, but can't seem to work out how to do it. As an example, I tried the following: ?php function addPrefix( $matches ) { static $line = 1; return $line++ . $matches[0]; } $url = split(\?, $_GET['url']); $source = highlight_file($url[0], true); $source = preg_replace_callback(`\br \/\`U, addPrefix, $source); echo $source; ? which is called whenever the use user on a link like a href=show-source.php?url=index.phpShow Source/a The problem is that my regex does not correcly match the lines. Anyone got any bright ideas? -- Alan McFarlane -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php