Basically i have a script that outputs a list of information about a file, Here is a sample

File Size:15423 bytes
File Name:Test.txt
File Location:C:\home\Test.txt
File MD5 Hash:1caf4a063526d84cd5b1ae6383a426d6
File SHA1 Hash:e79bfba095a8531adcb855d7c286f5c3ca93a535

My problem is that i want to split the data into two columns, For example "File Size:" Never changes so i want that as one column and then "15423 bytes" in a second column but that value changes. Here is my current code to how it displays that data.

$trID_Log_File = "testFile.txt";
	if(file_exists($trID_Log_File) && filesize($trID_Log_File) > 0) {
		$h = fopen($trID_Log_File, "r");
		$contents = fread($h, filesize($trID_Log_File));
		fclose($h);

		if(!stristr($contents, "TrID is best suited to analyze binary files!")) {
			$content .= "<table cellspacing=\"0\" cellpadding=\"0\">\n";
			$content .= "<tbody>\n";
			$i = 0;
			$lines = explode("\n", $contents);
			foreach($lines as $line) {
				if(strlen($line) > 5) {
					$content .= "<tr>\n";
					$content .= "<td class=\"".($i % 2 == 0 ? "odd" : "even")."\" style=\"text-align: left; padding: 2px\">".$line."</td>\n";
					$content .= "</tr>\n";
					$i++;
				}
			}
			$content .= "</tbody>\n";
			$content .= "</table>\n";
			$content .= "<div class=\"border\"></div>\n";
		}
	}

Any help here?

Member Avatar for diafol

Split the $line again with ":" delimiter. So $part[0] is the label and $part[1] is the value.

commented: useful +4
commented: I got brownies +3

But for file location there is a ":" After the hdd letter wont it attempt to split "C" and "\" ?

this will read text file by line, but you can slip each line with explode

<?php
$txtfile = file('testfile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($txtfile as $t){
echo $t."<br><br>";
}
?>

But for file location there is a ":" After the hdd letter wont it attempt to split "C" and "\" ?

In this case use explode function to split. The $part[0] is your Label ad the remaining parts are your value. You can use count() to find the umber of parts of that array.

I have no idea how to implement any of these ideas, Any help?

Member Avatar for diafol

Sorry just read your reply to my post wrt explode on ":". Yes it will cause a problem if you have a ':' in the value. You can either re-join parts with an index > 0 or use preg_split.

$file_content = file_get_contents($file);
$lines = explode("\n",$file_content);
$output = "...do table tags and thead etc...";
foreach($lines as $line){
  $parts = preg_split("/:([^\/|^\\])/", $line);
  $output .= "<tr>{$parts[0]}<td></td><td>{$parts[1]}</td></tr>"; 
}
$output .= "... tidy up end table tags ...";

then echo out the output where nedded in your html:

<h3>Result</h3>
<?php echo $output;?>
<p>Blah blah blah</p>

//EDIT

I'm a complete idiot at regex, so if anybody can think of a better expression, feel free. My rationale was- split on ':' but NOT when followed by a '\' or a '/'.

foreach($lines as $line){
$parts = preg_split("/:([^\/|^\\])/", $line);
$output .= "<tr>{$parts[0]}<td></td><td>{$parts[1]}</td></tr>";
}

Use below instead of above:

foreach($lines as $line){
$parts = preg_split('!\s*+[:]{1}\s!', $line);
$output .= "<tr>{$parts[0]}:<td></td><td>{$parts[1]}</td></tr>";
}

It should work. Hope this help.

commented: a lesson in regex! :) +13
Member Avatar for diafol

Thanks Zero13, my regex didn't work anyway. I continue to learn... :)

<?php    
    $trID_Log_File = "testFile.txt";
    if(file_exists($trID_Log_File) && filesize($trID_Log_File) > 0) 
	{
		$h = fopen($trID_Log_File, "r");
		$contents = fread($h, filesize($trID_Log_File));
		fclose($h);
     
		if(!stristr($contents, "TrID is best suited to analyze binary files!")) 
		{
			$content .= "<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\">\n";
			$content .= "<tbody>\n";
			$i = 0;
			$lines = explode("\n", $contents);
			foreach($lines as $line) 
			{
				if(strlen($line) > 5) 
				{
					$line_arr=explode(":",$line);
					$right="";
					for($j=1;$j<count($line_arr);$j++)
					{
						$right=$right.$line_arr[$j];
					}
					$content .= "<tr>\n";
					$content .= "<td>".$line_arr[0]."</td>\n";
					$content .= "<td>".$right."</td>\n";
					$content .= "</tr>\n";
					$i++;
				}
    		}
			$content .= "</tbody>\n";
			$content .= "</table>\n";
			$content .= "<div class=\"border\"></div>\n";
    	}
    }
?>
<?php    
    $trID_Log_File = "testFile.txt";
    if(file_exists($trID_Log_File) && filesize($trID_Log_File) > 0) 
	{
		$h = fopen($trID_Log_File, "r");
		$contents = fread($h, filesize($trID_Log_File));
		fclose($h);
     
		if(!stristr($contents, "TrID is best suited to analyze binary files!")) 
		{
			$content .= "<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\">\n";
			$content .= "<tbody>\n";
			$i = 0;
			$lines = explode("\n", $contents);
			foreach($lines as $line) 
			{
				if(strlen($line) > 5) 
				{
					$line_arr=explode(":",$line);
					$right="";
					for($j=1;$j<count($line_arr);$j++)
					{
						$right=$right.$line_arr[$j];
					}
					$content .= "<tr>\n";
					$content .= "<td>".$line_arr[0]."</td>\n";
					$content .= "<td>".$right."</td>\n";
					$content .= "</tr>\n";
					$i++;
				}
    		}
			$content .= "</tbody>\n";
			$content .= "</table>\n";
			$content .= "<div class=\"border\"></div>\n";
    	}
    }
?>

Thank you that is working fine.
Just another question, My current output displays all the files information Plus a bit of data which i want to remove it's a URL and looks like this
"URL=http://********/result.php?id=aac15_bjbo3" How do i go about removing this from my output? It's at the very bottom so it's the last bit of data.

preg_replace('/(URL=http:\/\/).\s*?/', '', $your_url);

Not tested yet. You can use 'preg_replace' regular expression function, and google can help you more detail. Hope this help.

Thank you that is working fine.
Just another question, My current output displays all the files information Plus a bit of data which i want to remove it's a URL and looks like this
"URL=http://********/result.php?id=aac15_bjbo3" How do i go about removing this from my output? It's at the very bottom so it's the last bit of data.

Sorry i can't get your question. Please re-post this with some more explanation with output you required.

Sorry i can't get your question. Please re-post this with some more explanation with output you required.

Here is my current output

http://gyazo.com/8fe8b4ef3f819da02f5090eec65ecb41.png

You see at the bottom of the table it shows

"URL=http //scan4you.net/result.php?id=24e80_bjbj7"

I need this to be removed from the output, And i also need a way of changing the color of the text for example, If the right column on the table displays "OK" I need it to be green but anything else must be red except for the text in the first column. I know im asking allot but i have no idea on how to do this part for my site.

Oh Ok.. Try this

<?php    
    $trID_Log_File = "testFile.txt";
    if(file_exists($trID_Log_File) && filesize($trID_Log_File) > 0) 
	{
		$h = fopen($trID_Log_File, "r");
		$contents = fread($h, filesize($trID_Log_File));
		fclose($h);
     
		if(!stristr($contents, "TrID is best suited to analyze binary files!")) 
		{
			$content .= "<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\">\n";
			$content .= "<tbody>\n";
			//$i = 0;
			$lines = explode("\n", $contents);
			//foreach($lines as $line) 
			for($i=0;$i<count($lines)-1;$i++)
			{
				if(strlen($lines[$i]) > 5) 
				{
					$line_arr=explode(":",$lines[$i]);
					$right="";
					for($j=1;$j<count($line_arr);$j++)
					{
						$right=$right.$line_arr[$j];
					}
					$content .= "<tr>\n";
					$content .= "<td>".$line_arr[0]."</td>\n";
					$content .= "<td". if($right=="OK") { echo " style='color:#00ff00'"; }.">".$right."</td>\n";
					$content .= "</tr>\n";
					//$i++;
				}
    		}
			$content .= "</tbody>\n";
			$content .= "</table>\n";
			$content .= "<div class=\"border\"></div>\n";
    	}
    }
?>

Oh Ok.. Try this

<?php    
    $trID_Log_File = "testFile.txt";
    if(file_exists($trID_Log_File) && filesize($trID_Log_File) > 0) 
	{
		$h = fopen($trID_Log_File, "r");
		$contents = fread($h, filesize($trID_Log_File));
		fclose($h);
     
		if(!stristr($contents, "TrID is best suited to analyze binary files!")) 
		{
			$content .= "<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\">\n";
			$content .= "<tbody>\n";
			//$i = 0;
			$lines = explode("\n", $contents);
			//foreach($lines as $line) 
			for($i=0;$i<count($lines)-1;$i++)
			{
				if(strlen($lines[$i]) > 5) 
				{
					$line_arr=explode(":",$lines[$i]);
					$right="";
					for($j=1;$j<count($line_arr);$j++)
					{
						$right=$right.$line_arr[$j];
					}
					$content .= "<tr>\n";
					$content .= "<td>".$line_arr[0]."</td>\n";
					$content .= "<td". if($right=="OK") { echo " style='color:#00ff00'"; }.">".$right."</td>\n";
					$content .= "</tr>\n";
					//$i++;
				}
    		}
			$content .= "</tbody>\n";
			$content .= "</table>\n";
			$content .= "<div class=\"border\"></div>\n";
    	}
    }
?>

I'm given this error

Parse error: syntax error, unexpected T_IF in /home/xblhacke/public_html/tagknife/scan/scan.php on line 113
Member Avatar for diafol

I can't believe that you're not using preg_split for this. Zero has a working regex.

Anyway, why the: $i<count($lines)-1? I didn't get the take the last line off. The < takes care that the numbers tally.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.