i have a php code that read text files and allow the user to make a search then it display the result in an html table that contains filename and line number each file can handle multiple lines.
all txt files composed of 10 categories where each category contains some text in it.
category 1
0.74 ±
0.18 ° C in the 100 years ended 2005. According to the IPCC, "most of the >observed increase in global temperature since the middle of the 20th century is >largely due to increased greenhouse gases (greenhouse gases) By humans.
Most of the sun's rays penetrate the atmosphere to reach the earth's surface. >Most of these rays are located at the visible wavelength (the sun's light that >the eye sees). Therefore, the Earth's surface is heated during the day. The >earth can not fully absorb solar energy. If this happens, the Earth's >temperature will reach the melting point , And the end of life on them
category 2
The earth is heated to absorb the heat of the sun, then the surface of the >earth returns the absorbed energy energy back into the atmosphere, and in the >absence of any complex effects, thermal equilibrium occurs. In this case, the >earth's surface temperature is about -2.3 ° C, and the energy that the Earth is >transmitting is infrared (this is contrary to reality).
In fact, complexities, where the atmosphere holds part of the infrared; because >of the particles of carbon dioxide and water vapor that absorbs most of the >infrared radiation, and the part that is not absorbed out of the atmosphere >into space, and the absorbing part of the infrared The atmosphere has a >temperature rise
the expected result:
i know that i must includes these categories in an array and while the system read read the files it make a compare with the array's element once it found a match it display the category name in second row under the line number.
i do not know how to write this idea in php can anyone help me ?
code:
<?php
$result = [];
if(isset($_POST["search"]))
{
$search =$_POST['name'];
echo "the word $search exist: <br><br>";
foreach(glob($_SERVER['DOCUMENT_ROOT']."/readfiletest/*.txt") as $txts)
{
$line = 1;
$temp = [];
$myFileLink = fopen($txts, 'r');
while(!feof($myFileLink))
{
$myFileContents = fgets($myFileLink);
if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
{
$temp['filename'] = basename ($txts);
foreach($matches[1] as $match)
{
$temp['lines'][] = $line;
}
}
++$line;
}
fclose($myFileLink);
$result[] = $temp;
}
//display the table
echo '<table class = "minimalistBlack" border=2>';
$filenameHtml = '<tr>';
$lineNumberHtml = '<tr>';
foreach ($result as $item)
{
$filename = isset($item['filename']) ? $item['filename'] : '';
$lines = isset($item['lines']) ? implode(',',$item['lines']) : '';
//$filenameHtml .= "<th>$filename</th>";
$new_filename = str_replace('.txt', '.pdf',$filename);
$filenameHtml .= !empty($filename) ? "<th><a href ='".$new_filename."'target='_blank'>$filename</a></th>" : ''; // added !empty()
$lineNumberHtml .= !empty($lines) ? "<td>$lines</td>" : ''; // added !empty()
}
$filenameHtml .= '</tr>';
$lineNumberHtml .= '</tr>';
echo $filenameHtml.$lineNumberHtml;
echo '</table>';
}
?>
<html>
<head>
</head>
<meta http-equiv="Content-Language" content="ar-sa">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
#form {
background: -webkit-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
background: -moz-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
background: linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
margin: auto;
width: 200px;
height: 200px;
position: absolute;
font-family: Tahoma, Geneva, sans-serif;
font-size: 14px;
font-style: italic;
line-height: 24px;
font-weight: bold;
color: #09C;
text-decoration: none;
border-radius: 10px;
padding: 10px;
border: 1px solid #999;
border: inset 1px solid #333;
-webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
}
</style
<body>
<div id = "form">
<form action="index.php" method="post">
<h1 align =center > Search Form </h1>
<p>enter your string <input type ="text" id = "idName" name="name" /></p>
<p align =center ><input type ="Submit" name ="search" value= "Search" /></p>
</form>
</div>
</body>
</html>