Forum Moderators: coopster

Message Too Old, No Replies

PHP output question

         

andrewsmd

2:57 pm on Sep 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I have a rather complex problem (at least I think). I have a php web page that searches files based on a user defined path (i.e. c:\some folder\*.txt). Then it displays the files in a nice little output. An example is let's say we searched with the previous pattern and these files exist in "some folder" "temp.txt" "temp2.txt" "temp3.doc" Now it would display the contents of temp and temp2 but not temp3 because it is .doc. Anyways, when I use it sometimes I would like to search my other php files and display the output in the same format. The problem is where they are php files, when I echo the contents I get lots of errors because it is echoing php. This is the basics of how I output the files.
//this function takes in an array of file names and prints out their content
function contentOutput($arrFiles, $path, $fileNameSearch){

echo("<p class = 'text'>Path: $path<br><br>File Name Search Text: ".getFileName($fileNameSearch)."<br>Number of files matching criteria: ".count($arrFiles)."
<br>File Name(s):</p>");

foreach($arrFiles as $key => $i){

if(($key % 2) == 1){

$div = "output3";

}//if $key

else{

$div = "output2";

}//else

echo("<br><br><br><div id = '{$div}'><p class = 'text'>File Name: ".getFileName($i)."</p>");

$lines = file($i);

foreach($lines as $j){

echo("<p class = 'text'>$j</p>\n");

}//foreach

echo("</div>");

}//$foreach

}//$contentOutput

This function takes in an array of files the path they are in and the search text. It then echos a div and stores the contents of a file into an array. Then it prints out every line of that array. Now my problem is in the line echo("<p class = 'text'>$j</p>\n"); because $j is the contents of a file and if it's contents are PHP itself it will error (for obvious reasons). What I am wondering is if there is any way to echo it without parsing anything without writing a function that searches each line for anything that would screw up php (i.e. a ?> or <?php) and then escaping it? Thanks

Anyango

3:32 pm on Sep 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



because $j is the contents of a file and if it's contents are PHP itself it will error

Try str_replace on $j. replace "<?" and "<?php" and "?>" with empty string, or mabe "PHP CODE STARTS", "PHP CODE ENDS"

andrewsmd

4:30 pm on Sep 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if <?PHP are the only things that would make it error. I was wondering if there is a function or some kind of print method that will just print the contents as a string and ignore what is in it.

Anyango

4:35 pm on Sep 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try wrapping $j with htmlentities

echo htmlentities($j);

might not be the best solution but should work

andrewsmd

4:54 pm on Sep 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That was exactly what I was looking for. I don't care what the source looks like. Thanks,