Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Jul 17, 2002, 16:41   #1
Travis
Super Ninja Monkey
 
Travis's Avatar
 
Join Date: Dec 2001
Location: Sioux City, Iowa
Posts: 693
PHP Tennis: Free-for-all

Ok you can find the complete rules here. Make sure you read them carefully. If you break a rule you lose, end of story.

ok heres my serve:
PHP Code:

<?  

/*
    Source code view with syntac higlight using
    highlight_string() PHP4 function.
    by : iwyrobi [iwyrobi@yahoo.com]
  */  

function source($source)  
{   
  
clearstatcache();                               
  if(!
file_exists($source))                       
  {  
        return
1;  
  }  
  
$handle=fopen($source,"r"); // open source code
  
$size=filesize($source);
  
$temp=fread($handle,$size);
  echo
"<B><FONT size=2>";  
  
$colorcode=highlight_string($temp);  // give the color
  
echo "</font></b>";   
  return
0;  
}  
?>
They also had the usage for it but I don't think thats needed. You can find this script here.

The Max Change is 7 lines.
The Red Line is 6 lines. So you can't go above 29 lines or below 17 lines.
You have 24 hours to make a return from the time of the last one.

Have fun!
__________________
Travis Watkins - Hyperactive Coder
My Blog: Realist Anew
Projects: Alacarte - Gnome Menu Editor
Travis is offline   Reply With Quote
Old Jul 17, 2002, 18:35   #2
Travis
Super Ninja Monkey
 
Travis's Avatar
 
Join Date: Dec 2001
Location: Sioux City, Iowa
Posts: 693
No one came in here and added security functions or a nice user interface yet? *hint*

You people are ignoring me aren't you!





__________________
Travis Watkins - Hyperactive Coder
My Blog: Realist Anew
Projects: Alacarte - Gnome Menu Editor
Travis is offline   Reply With Quote
Old Jul 18, 2002, 05:10   #3
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
24 hours allows me to post.


PHP Code:

<?  

/*
    Source code view with syntac higlight using
    highlight_string() PHP4 function.
    by : iwyrobi [iwyrobi@yahoo.com]
  */  

function source($source)  
{   
  
clearstatcache();                               
  if(!
file_exists($source))                       
  {  
        return
"This file does not exist!";  
  }  
  
$handle=fopen($source,"r"); // open source code
  
$size=filesize($source);
  
$temp=fread($handle,$size);
  echo
"<B><FONT size=2>";  
  
$colorcode=highlight_string($temp);  // give the color
  
echo "</font></b>";   
  return
0;  
}  
?>
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 18, 2002, 06:21   #4
voostind
SitePoint Evangelist
 
Join Date: Oct 2001
Posts: 592
How about dropping all those useless variables?

PHP Code:

/* 

    Source code view with syntax higlight using
    highlight_string() PHP4 function.
    by : iwyrobi [iwyrobi@yahoo.com]
*/  
function source($source)
{
    
clearstatcache();                               
    if (!
file_exists($source))                       
    {  
        return
"This file does not exist!";  
    }
    echo
"<B><FONT size=2>";
    
highlight_string(join('', file($source)));
    echo
"</FONT></B>";
    return
0;
}
voostind is offline   Reply With Quote
Old Jul 18, 2002, 08:47   #5
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
That doesn't appear to do the original function's idea, but it could just be me...

Wasn't the original one supposed to give the source of the file in $source?

Like:

PHP Code:

source("somephpfile.php"); 

The oen you just posted only does it for the code you have there... is that considered a fatal error?

Thanks,
~someonewhois
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 18, 2002, 08:48   #6
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
Plus, it doesn't highlight the text...


So I'm juts assuming that's a fatal error? Can somebody confirm this before anythign else happens?

Thanks,
~someonewhois
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 18, 2002, 09:10   #7
voostind
SitePoint Evangelist
 
Join Date: Oct 2001
Posts: 592
What exactly do you mean? Have you tried it? This code reads the file in $source and prints it as HTML, syntax highlighted. I thought that was the idea?

(For clarity: it works on PHP 4.2.1, because that is what I tried it on.)

And by the way: I just found out that this is also possible:

PHP Code:

function source($source)

{
    
clearstatcache();                               
    if (!
file_exists($source))                       
    {  
        return
"This file does not exist!";  
    }
    echo
"<B><FONT size=2>";
    
highlight_file($source);
    echo
"</FONT></B>";
    return
0;
}
Vincent
voostind is offline   Reply With Quote
Old Jul 18, 2002, 09:47   #8
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
PHP Code:

function source($source)

{
    
clearstatcache();                               
    if (!
file_exists($source))                       
    {  
        return
"This file does not exist!";  
    }
    echo
"<B><FONT size=2>";
    
highlight_file($source);
    echo
"</FONT></B>";
    return
0;
}
No its' not, that' sunder 17 lines which is the red line..


ANd this is what I did last time:


PHP Code:

/* 

    Source code view with syntax higlight using
    highlight_string() PHP4 function.
    by : iwyrobi [iwyrobi@yahoo.com]
*/  
function source($source)
{
    
clearstatcache();                               
    if (!
file_exists($source))                       
    {  
        return
"This file does not exist!";  
    }
    echo
"<B><FONT size=2>";
    
highlight_string(join('', file($source)));
    echo
"</FONT></B>";
    return
0;
}


source("test.php");
And it showed this:

Quote:
/* Source code view with syntax higlight using highlight_string() PHP4 function. by : iwyrobi [iwyrobi@yahoo.com] */ function source($source){ clearstatcache(); if (!file_exists($source)) { return "This file does not exist!"; } echo "<B><FONT size=2>"; highlight_string(join('', file($source))); echo "</FONT></B>"; return 0;} source("test.php");
All on one line..

Thanks,
~someonewhois
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 18, 2002, 09:53   #9
voostind
SitePoint Evangelist
 
Join Date: Oct 2001
Posts: 592
What version of PHP are you using? Because on my machine(s) with PHP 4.2.1, it works great...

But I guess I'm out anyway, with that last post under the red line.

Greetings

Vincent
voostind is offline   Reply With Quote
Old Jul 18, 2002, 09:59   #10
Wilco
SitePoint Zealot
 
Join Date: Jan 2002
Posts: 167
Comments dont count as lines, if you read the rules so the start part was wrong. its 17 lines, but we will keep redline as 6, so Min: 11 Max: 23


PHP Code:

<?

function source($source)
{
    
clearstatcache();                               
    echo
"<B><FONT size=2>";
    (!
file_exists($source)) ? highlight_file(join('\n',file($source)) : return "This file does not exist";
    echo
"</FONT></B>";
    return
0;
}
?>


I was counting tags, so that is 11, wether it works or not im not sure, as I cant test it from here


that was edited from voostind's post btw. and the edit is too add this line
Wilco is offline   Reply With Quote
Old Jul 18, 2002, 10:43   #11
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
PHP Code:

<?

function source($source)
{
    
clearstatcache();                               
    echo
"<B><FONT size=2>";
    (!
file_exists($source)) ? highlight_file(join('\n',file($source)) : return "This file does not exist";
    echo
"</FONT></B>";
    return
0;
}
?>
That loosk like 10 to me, not 11, so I think you loose.


Wilco loses, voostind wins, as Voostind's now works.

Thanks,
~someonewhois
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 18, 2002, 10:45   #12
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
Voostind, when you serve, please don't choose a source viewer, as we have had this one and t his one:

http://sitepointforums.com/showthrea...threadid=67541


That one was a lot more advanced than the one we juts made..


http://sowibb.ibboards.com/phpedit.php is the example, just type "phpedit.php" for an example.


It uses a form and it gives line numbers.

Thanks,
~someonewhois
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 18, 2002, 11:26   #13
Travis
Super Ninja Monkey
 
Travis's Avatar
 
Join Date: Dec 2001
Location: Sioux City, Iowa
Posts: 693
Well this certainly picked up! Can't wait to see what Vincent will use for his. I hope its a good one!
__________________
Travis Watkins - Hyperactive Coder
My Blog: Realist Anew
Projects: Alacarte - Gnome Menu Editor
Travis is offline   Reply With Quote
Old Jul 18, 2002, 13:39   #14
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
I don't care if it's useful or useless, as long as it goes on for 5+ volleys with no disputes..
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 18, 2002, 14:30   #15
Wilco
SitePoint Zealot
 
Join Date: Jan 2002
Posts: 167
then i think you will be dissapointed Some1


and damn, im sure that was 11
should have just halved the long line

did anyone test it to see if it worked?
Wilco is offline   Reply With Quote
Old Jul 18, 2002, 15:22   #16
voostind
SitePoint Evangelist
 
Join Date: Oct 2001
Posts: 592
Ok, so here is my serve:

PHP Code:

function showdir($prefix = '')

{
    
$files = array();
    
$dh    = opendir('.');
    while ((
$file = readdir($dh)) !== false)
    {
        if (
$file != '.' && $file != '..')
        {
            
array_push($files, $file);
        }
    }
    
closedir($dh);
    
sort($files);
    if (empty(
$prefix)) echo '<pre>', realpath('.'), "\n";
    
$max = count($files) - 1;
    for (
$i = 0; $i < $max + 1; $i++)
    {
        echo
$prefix, (($i == $max) ? '`' : '|'), '-- ', $files[$i], "\n";
        if (
is_dir($files[$i]) && chdir($files[$i]))
        {
            
showdir(($i == $max) ? $prefix . '   ' : $prefix . '|   ');
            
chdir('..');
        }
    }
    if (empty(
$prefix)) echo "</pre>\n";
}
This function mimics the behavior of the Unix 'tree' command on the current directory. Example:

PHP Code:

chdir('/path/to/required/tree');

showdir();
There are a couple of things that could be a lot better. So go ahead and try to volley!

Vincent
voostind is offline   Reply With Quote
Old Jul 18, 2002, 16:08   #17
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Just butting in for a moment with a suggestion for another "high risk" match for a later date, using PHP's custom error handlers: http://www.php.net/manual/en/functio...or-handler.php
HarryF is offline   Reply With Quote
Old Jul 18, 2002, 18:16   #18
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
Quote:
Originally posted by Wilco
then i think you will be dissapointed Some1
Fat chance?
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 19, 2002, 14:23   #19
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
Just to keep ti going, may as well...


PHP Code:

<?php

function showdir($prefix = '')
{
    
$files = array();
    
$dh    = opendir('.');
    while ((
$file = readdir($dh)) !== false)
    {
        if (
$file != '.' && $file != '..')
        {
            
array_push($files, $file);
        }
    }
    
closedir($dh);
    
sort($files);
    if (empty(
$prefix)) echo '<pre>', realpath('.'), "\n";
    
$max = count($files) - 1;
    for (
$i = 0; $i < $max + 1; $i++)
    {
        echo
$prefix, (($i == $max) ? '`' : '|'), '-- ', $files[$i], "\n";
        if (
is_dir($files[$i]) && chdir($files[$i]))
        {
            
showdir(($i == $max) ? $prefix . '   ' : $prefix . '|   ');
            
chdir('..');
        }
    }
    if (empty(
$prefix)) echo "</pre>\n";
}

If HarryF would so kindly update the rules, as you CAN have more than one semi collen if it's in a for loop as far as I'm concerned...
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 19, 2002, 14:31   #20
hex
SitePoint Enthusiast
 
hex's Avatar
 
Join Date: Jul 2002
Location: Cambs, UK
Posts: 59
Colour-coded. Blue for folders, green for files.
PHP Code:

function showdir($prefix = '')

{
    
$files = array();
    
$dh    = opendir('.');
    while ((
$file = readdir($dh)) !== false)
    {
        if (
$file != '.' && $file != '..')
        {
            
array_push($files, $file);
        }
    }
    
closedir($dh);
    
sort($files);
    if (empty(
$prefix)) echo '<pre>', realpath('.'), "\n";
    
$max = count($files) - 1;
    for (
$i = 0; $i < $max + 1; $i++)
    {
        if (
is_dir($files[$i]) && chdir($files[$i]))
        {
            echo
$prefix, (($i == $max) ? '`' : '|'), '-- <font color="#0000bb">', $files[$i], "</font>\n";
            
showdir(($i == $max) ? $prefix . '   ' : $prefix . '|   ');
            
chdir('..');
        } else {
            echo
$prefix, (($i == $max) ? '`' : '|'), '-- <font color="#007700">', $files[$i], "</font>\n";
        }
    }
    if (empty(
$prefix)) echo "</pre>\n";
}
__________________
:: David Speake
:: EvilWalrus.com Content Dev.
:: david@evilwalrus.com
:: david.bigbluebang.com
hex is offline   Reply With Quote
Old Jul 19, 2002, 14:32   #21
hex
SitePoint Enthusiast
 
hex's Avatar
 
Join Date: Jul 2002
Location: Cambs, UK
Posts: 59
Crap that breaks the characters-per-line limit.
__________________
:: David Speake
:: EvilWalrus.com Content Dev.
:: david@evilwalrus.com
:: david.bigbluebang.com
hex is offline   Reply With Quote
Old Jul 19, 2002, 16:27   #22
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
So.... do I win and you loose and everybody else doesn't get anything?
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 19, 2002, 16:33   #23
voostind
SitePoint Evangelist
 
Join Date: Oct 2001
Posts: 592
Well, hex certainly lost, and this game should be over, because those are the rules.

On the other hand, I don't think someonewhois won either, because the only change he made to the original script was to add '<?php' in front of it. Lacking the closing '?>', that probably won't run...

Vincent
voostind is offline   Reply With Quote
Old Jul 20, 2002, 05:11   #24
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
No, lcaking the <?php won't run, but on most computers you don't need a ?>...


The front one is more important, and I can demostrate that it works without ?>... but doesn't work without <?php..

Thanks,
~someonewhois
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 20, 2002, 23:08   #25
Travis
Super Ninja Monkey
 
Travis's Avatar
 
Join Date: Dec 2001
Location: Sioux City, Iowa
Posts: 693
*bump*

someonewhois, just start a new one. no more fighting!
__________________
Travis Watkins - Hyperactive Coder
My Blog: Realist Anew
Projects: Alacarte - Gnome Menu Editor
Travis is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 21:10.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved