Read a text file and print pre-defined html when specific keywords are found How can I code this in a better way, in one file? Output must be more than one (if there is more that one status_update with the given word &quot;online or performancedegradation&quot;), like:

#online
System is operating at peak performance.

#directonly
Traffic is bypassing Smart CDN system, and going directly to websites.


&lt;?php
$filename = &quot;export_file_system_status_output&quot;;   
$status_update = &quot;online&quot;;  
$handle = fopen($filename, &quot;r&quot;);  
$file = fread($handle,filesize($filename));  
if (preg_match('#' . $status_update . '#', $file, $match)) {  
echo &quot;&lt;div id=badge_subpage_system&gt;
                                        &lt;h5&gt;#online&lt;/h5&gt;
                                        &lt;p&gt;System is operating at peak performance.&lt;/p&gt;
                                    &lt;/div&gt;&quot;;
}  
?&gt;

&lt;?php
$filename = &quot;export_file_system_status_output&quot;;   
$status_update = &quot;performancedegradation&quot;;  
$handle = fopen($filename, &quot;r&quot;);  
$file = fread($handle,filesize($filename));  
if (preg_match('#' . $status_update . '#', $file, $match)) {  
echo &quot;&lt;div id=badge_subpage_system&gt;
                                        &lt;h5&gt;#performancedegradation&lt;/h5&gt;
                                        &lt;p&gt;Performance is slower than normal.&lt;/p&gt;
                                    &lt;/div&gt;&quot;;
}
?&gt;
                                    
&lt;?php
$filename = &quot;export_file_system_status_output&quot;;   
$status_update = &quot;directonly&quot;;  
$handle = fopen($filename, &quot;r&quot;);  
$file = fread($handle,filesize($filename));  
if (preg_match('#' . $status_update . '#', $file, $match)) {  
echo &quot;&lt;div id=badge_subpage_system&gt;
                                        &lt;h5&gt;#directonly&lt;/h5&gt;
                                        &lt;p&gt;Traffic is bypassing Smart CDN system, and going directly to websites.&lt;/p&gt;
                                     &lt;/div&gt;&quot;;
}
?&gt; 

It must sort by first status_update found:

online
#online

System is operating at peak performance.


then performancedegradation
#directonly

Traffic is bypassing Smart CDN system, and going directly to websites.
I'll give you a hint. Try using a foreach loop:
http://www.php.net/manual/en/control-structures.foreach.php

You can store all your files and associated information in an associative array, such as:

$files = array(
  0 =&gt; array(
    'filename' =&gt; "export_file_system_status_output";   
    'status_update' =&gt; "online";
  ),
  1 =&gt; array(
    'filename' =&gt; "another_file";   
    'status_update' =&gt; "offline";
  )
);


You should really try this on your own :) If you don't get it, I'll give you a more complete answer :)

On a side note, your HTML is not valid. Use classes instead of ids when possible, and use quotes:

&lt;div class="badge_subpage_system"&gt;
  &lt;h5&gt;#performancedegradation&lt;/h5&gt;
  &lt;p&gt;Performance is slower than normal.&lt;/p&gt;
&lt;/div&gt;


Hope this helps :)

EDIT:

Here's what your final code will look like, with the array I posted above:

foreach($files as $file)
{
  $handle = fopen($file['filename'], "r");
  $file = fread($handle,filesize($file['filename']));
  if (preg_match('#' . $file['status_update'] . '#', $file, $match))
  {
    echo '&lt;div id="badge_subpage_system"&gt;&lt;h5&gt;#performancedegradation&lt;/h5&gt;&lt;p&gt;Performance is slower than normal.&lt;/p&gt;&lt;/div&gt;';
  }
}