I want to open a text file that contains some HTML code, for example:
<table border="3">
<tr>
<td rowspan="3">Your text/code here</td>
<td colspan="2">Your text/code here</td></tr>
<tr>
<td>Your text/code here</td>
<td>Your text/code here</td>
</tr>
<tr>
<td>Your text/code here</td>
<td>Your text/code here</td>
</tr>
<tr>
<td align="center" colspan="3">Your text/code here</td>
</tr>
</table>
I want to do this using an SSI tag, for example:
<!--#include virtual="/data.php?sample=example1.txt" -->
where data.php is the script and sample is the name that holds the value of the file I want to open (example1.txt)
what I would like is for the html output look like the php tag does on this board:
<table border="3">
<tr>
<td rowspan="3">Your text/code here</td>
<td colspan="2">Your text/code here</td></tr>
<tr>
<td>Your text/code here</td>
<td>Your text/code here</td>
</tr>
<tr>
<td>Your text/code here</td>
<td>Your text/code here</td>
</tr>
<tr>
<td align="center" colspan="3">Your text/code here</td>
</tr>
</table>
I am a total PHP noob, any help appreciated. I don't want to change the web page to a php page which is why I prefer to do this with an SSI tag if possible. The webpage the SSI tag is on has already printed out an HTTP header if that makes a difference.simple...your still including a php document,no?
just use:
$sample = $_GET["sample"];
if(file_exists($sample))
{
highlight_file($sample);
}
else
{
echo 'Unable to open file';
}
that will check if the file exists in the same dir as the script,if so it will use php's highlight_file(); function to perform syntax highlighting on the file...otherwise it will display 'Unable to open file'.
hows that there Kevin?
see also: <!-- m --><a class="postlink" href="http://www.php.net/highlight-fileyou">http://www.php.net/highlight-fileyou</a><!-- m --> also have
highlight_string( $file );
or
show_source( $file ); //alias for highlight_file
but I think unless you have the opening php tags <? it won't color code them. one of those does that and I think it is the string one.
also be carefull Kev, if it is not coded right a user could highlight files you don't expect, like system files or something.Originally posted by willamoose
simple...your still including a php document,no?
just use:
$sample = $_GET["sample"];
if(file_exists($sample))
{
highlight_file($sample);
}
else
{
echo 'Unable to open file';
}
that will check if the file exists in the same dir as the script,if so it will use php's highlight_file(); function to perform syntax highlighting on the file...otherwise it will display 'Unable to open file'.
hows that there Kevin?
see also: <!-- m --><a class="postlink" href="http://www.php.net/highlight-file">http://www.php.net/highlight-file</a><!-- m --> thats not working, its just colorizing the entire output the same color, black.
<!-- m --><a class="postlink" href="http://www.e-pixs.com/htmlfiles/">http://www.e-pixs.com/htmlfiles/</a><!-- m -->
you can see the html in the gray boxKev, this board actually uses the highlight_string($code);Originally posted by scoutt
Kev, this board actually uses the highlight_string($code);
OK, then what parameter do I need to include in the highlight_string() function:
<?php
$sample = $_GET["sample"];
if(file_exists($sample))
{
highlight_string();
}
else
{
echo 'Unable to open file';
}
?>you would have to open it up first.
<?php
$sample = $_GET["sample"];
if(file_exists($sample))
{
$fp = fopen($sample);
highlight_string($fp);
}
else
{
echo 'Unable to open file';
}
?>
I believe that should work, I haven't tested it yet. I can't remember if the file pointer is a resource ID or if it is actually the contents. or I guess you could do this
<?php
$sample = $_GET["sample"];
if(file_exists($sample))
{
$fp = file_get_contents($sample);
highlight_string($fp);
}
else
{
echo 'Unable to open file';
}
?>your first snippet gets an error:
Warning: fopen() expects at least 2 parameters, 1 given in /home/e-pixsco/public_html/htmlfiles/data.php on line 5
the second snippet does the same thing as williams code, it just colors the entire contents of the file the same color, black. It does not do syntax highlighting like the php tag does on this forum.$fp = fopen($sample, 'r');
try that one
I do this on my site and unless I use the <? tags I don't get color. so not sure how this forum does it. might have to look and get back to you Originally posted by scoutt
$fp = fopen($sample, 'r');
try that one
I do this on my site and unless I use the <? tags I don't get color. so not sure how this forum does it. might have to look and get back to you
no, that returns:
Resource id #2
I went ahead and wrote a short Perl script to do the syntax highlighting. There is no built in module for syntax highlighting with Perl which is why I was hoping to just use a php file to do it. But the Perl script I wrote is working good and I don't have to mess around getting the php output to validate as html 4.01 since my Perl script will insert valid html 4.01 code as is.
Thanks for the help guys, I really do appreciate it.ahh, it does insert the <? in the code before it outputs it. then after this code they strip the <? out.
if (!strpos($code,"<?") and substr($code,0,2)!="<?") {
$code="<?\n".trim($code)."\n?>";
$addedtags=1;
}
but cool you made one in perl, I do have a script that will make it validate better.here's the Perl script if it interests anyone:
#!/usr/bin/perl -wT
use CGI qw(-no_xhtml);
use strict;
my $q = new CGI;
print $q->header();
my $data = "/home/e-pixsco/public_html/htmlfiles/examples/";
my $example = $q->param('sample');
die "Unable to continue.\n" unless ($example =~ m/^([\w.-]+)$/);# untaint the path
$example = $1;
open (FILE,"$data$example");
while (<FILE>){
$_ =~ s/(=\"[a-zA-Z0-9\;\-\/\_\,\.\'\=\:\?\!\#\+\s]*\")/\[span class="attribs"\]$1\[\/span\]/g;
$_ =~ s/</\[span class="left_tags"\]<\[\/span\]\[span class="tags"\]/g;
$_ =~ s/>/\[\/span\]\[span class="right_tags"\]>\[\/span\]/g;
$_ =~ tr/][/></;
print $_;
}
close (FILE);
exit(0)
<table border="3">
<tr>
<td rowspan="3">Your text/code here</td>
<td colspan="2">Your text/code here</td></tr>
<tr>
<td>Your text/code here</td>
<td>Your text/code here</td>
</tr>
<tr>
<td>Your text/code here</td>
<td>Your text/code here</td>
</tr>
<tr>
<td align="center" colspan="3">Your text/code here</td>
</tr>
</table>
I want to do this using an SSI tag, for example:
<!--#include virtual="/data.php?sample=example1.txt" -->
where data.php is the script and sample is the name that holds the value of the file I want to open (example1.txt)
what I would like is for the html output look like the php tag does on this board:
<table border="3">
<tr>
<td rowspan="3">Your text/code here</td>
<td colspan="2">Your text/code here</td></tr>
<tr>
<td>Your text/code here</td>
<td>Your text/code here</td>
</tr>
<tr>
<td>Your text/code here</td>
<td>Your text/code here</td>
</tr>
<tr>
<td align="center" colspan="3">Your text/code here</td>
</tr>
</table>
I am a total PHP noob, any help appreciated. I don't want to change the web page to a php page which is why I prefer to do this with an SSI tag if possible. The webpage the SSI tag is on has already printed out an HTTP header if that makes a difference.simple...your still including a php document,no?
just use:
$sample = $_GET["sample"];
if(file_exists($sample))
{
highlight_file($sample);
}
else
{
echo 'Unable to open file';
}
that will check if the file exists in the same dir as the script,if so it will use php's highlight_file(); function to perform syntax highlighting on the file...otherwise it will display 'Unable to open file'.
hows that there Kevin?
see also: <!-- m --><a class="postlink" href="http://www.php.net/highlight-fileyou">http://www.php.net/highlight-fileyou</a><!-- m --> also have
highlight_string( $file );
or
show_source( $file ); //alias for highlight_file
but I think unless you have the opening php tags <? it won't color code them. one of those does that and I think it is the string one.
also be carefull Kev, if it is not coded right a user could highlight files you don't expect, like system files or something.Originally posted by willamoose
simple...your still including a php document,no?
just use:
$sample = $_GET["sample"];
if(file_exists($sample))
{
highlight_file($sample);
}
else
{
echo 'Unable to open file';
}
that will check if the file exists in the same dir as the script,if so it will use php's highlight_file(); function to perform syntax highlighting on the file...otherwise it will display 'Unable to open file'.
hows that there Kevin?
see also: <!-- m --><a class="postlink" href="http://www.php.net/highlight-file">http://www.php.net/highlight-file</a><!-- m --> thats not working, its just colorizing the entire output the same color, black.
<!-- m --><a class="postlink" href="http://www.e-pixs.com/htmlfiles/">http://www.e-pixs.com/htmlfiles/</a><!-- m -->
you can see the html in the gray boxKev, this board actually uses the highlight_string($code);Originally posted by scoutt
Kev, this board actually uses the highlight_string($code);
OK, then what parameter do I need to include in the highlight_string() function:
<?php
$sample = $_GET["sample"];
if(file_exists($sample))
{
highlight_string();
}
else
{
echo 'Unable to open file';
}
?>you would have to open it up first.
<?php
$sample = $_GET["sample"];
if(file_exists($sample))
{
$fp = fopen($sample);
highlight_string($fp);
}
else
{
echo 'Unable to open file';
}
?>
I believe that should work, I haven't tested it yet. I can't remember if the file pointer is a resource ID or if it is actually the contents. or I guess you could do this
<?php
$sample = $_GET["sample"];
if(file_exists($sample))
{
$fp = file_get_contents($sample);
highlight_string($fp);
}
else
{
echo 'Unable to open file';
}
?>your first snippet gets an error:
Warning: fopen() expects at least 2 parameters, 1 given in /home/e-pixsco/public_html/htmlfiles/data.php on line 5
the second snippet does the same thing as williams code, it just colors the entire contents of the file the same color, black. It does not do syntax highlighting like the php tag does on this forum.$fp = fopen($sample, 'r');
try that one
I do this on my site and unless I use the <? tags I don't get color. so not sure how this forum does it. might have to look and get back to you Originally posted by scoutt
$fp = fopen($sample, 'r');
try that one
I do this on my site and unless I use the <? tags I don't get color. so not sure how this forum does it. might have to look and get back to you
no, that returns:
Resource id #2
I went ahead and wrote a short Perl script to do the syntax highlighting. There is no built in module for syntax highlighting with Perl which is why I was hoping to just use a php file to do it. But the Perl script I wrote is working good and I don't have to mess around getting the php output to validate as html 4.01 since my Perl script will insert valid html 4.01 code as is.
Thanks for the help guys, I really do appreciate it.ahh, it does insert the <? in the code before it outputs it. then after this code they strip the <? out.
if (!strpos($code,"<?") and substr($code,0,2)!="<?") {
$code="<?\n".trim($code)."\n?>";
$addedtags=1;
}
but cool you made one in perl, I do have a script that will make it validate better.here's the Perl script if it interests anyone:
#!/usr/bin/perl -wT
use CGI qw(-no_xhtml);
use strict;
my $q = new CGI;
print $q->header();
my $data = "/home/e-pixsco/public_html/htmlfiles/examples/";
my $example = $q->param('sample');
die "Unable to continue.\n" unless ($example =~ m/^([\w.-]+)$/);# untaint the path
$example = $1;
open (FILE,"$data$example");
while (<FILE>){
$_ =~ s/(=\"[a-zA-Z0-9\;\-\/\_\,\.\'\=\:\?\!\#\+\s]*\")/\[span class="attribs"\]$1\[\/span\]/g;
$_ =~ s/</\[span class="left_tags"\]<\[\/span\]\[span class="tags"\]/g;
$_ =~ s/>/\[\/span\]\[span class="right_tags"\]>\[\/span\]/g;
$_ =~ tr/][/></;
print $_;
}
close (FILE);
exit(0)