I am using PHP to keep my webpage organized.

The directory looks like this:


Each file is very simple html doc with content containing a title tag, which I use to create a nav bar.

I want index.php to aggregate all the content from file1, file2 and file3, in addition to creating a nav bar up at the top.

The code I have achieves this successfully, but I'm not sure if I'm doing this in a practical way.

$domain = "domain/"
$dir = "folder/";
$files = scandir($dir);

// loop over all the files, saving their contents and ids to arrays
$contents = Array();
$titles = Array();
$ids = Array();
for ($x = 0; $x < sizeof($files); $x++) {   
  if ($files[$x] != "." && $files[$x] != "..") {
    $filename = $domain . $dir . $files[$x];
    $file = fopen($filename, "r");
    $data = file_get_contents($filename);
    array_push($contents, $data);

    $regex = '#<title>(.*?)<\/title>#';  
    preg_match($regex, $data, $match);
    $title = $match[1];
    array_push($titles, $title);

    $regex2 = '#( |-|,)#';
    $id = preg_split($regex2, $title)[0];   
    array_push($ids, $id);

// the first loop sets the nav bar
echo "<div id='navigation'>";
echo "<ul>";
for ($x = 0; $x < sizeof($contents); $x++) {
    echo "<li><a href='#" . $ids[$x] . "'>" . $titles[$x] . "</a></li>";    
echo "</ul>";
echo "</div>";

// this second loop sets the contents
echo "<div id='content'>";
for ($x = 0; $x < sizeof($contents); $x++) {
    echo "<div id='" . $ids[$x] . "'>" . $contents[$x] . "</div>";    
echo "</div>";

I like the idea of having all the content on one big page and the nav bar helps a lot when I'm viewing it on my phone.


  1. Are there any obvious problems with the way I am aggregating my files?
  2. Are there challenges I may encounter that I have not yet experienced?
  3. Is there a generally accepted way of doing this while achieving the same result?

1 Answer 1


Basically you need to remove the unused fopen() call and change ponderous operators to handy ones as for some reason you made a peculiar choice in favor of the former, like for vs. foreach, array_push vs simple assignment, etc. Also consider a cleaner way to output HTML

$pattern = "domain/folder/*.*";
$files = glob($pattern);

// loop over all the files, saving their contents and ids to arrays

$contents = [];
$titles = [];
$ids = [];
foreach ($files as $filename)
    $data = file_get_contents($filename);
    $contents[] = $data;

    preg_match('#<title>(.*?)<\/title>#', $data, $match);
    $titles[] = $match[1];

    $ids[] = preg_split('#( |-|,)#', $match[1])[0];
<div id='navigation'>
<?php foreach ($titles as $x => $title): ?>
    <li><a href="#<?=$ids[$x]?>"><?=$title?></a></li>
<?php endforeach ?>

<div id='content'>
<?foreach ($contents as $x => $content): ?>
    <div id="<?=$ids[$x]?>"><?=$content?></div>
<?php endforeach ?>
  • \$\begingroup\$ This is very interesting. I don't have a lot of experience with php so you've shown me quite a bit. In line 3 of your code I simply changed $dir to $pattern, and in line 18 I changed $title to $match[1], since $title no longer exists. I'm still reviewing it but the result is the same as before which is good. Are the overall mechanics of what I'm doing (looping once to capture data, then looping 2 more times to template the content) reasonable? \$\endgroup\$ Jan 28, 2019 at 0:38
  • \$\begingroup\$ Looping 2 times is the only acceptable way. Data preparation should be always separated from the output as output may vary \$\endgroup\$ Jan 28, 2019 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.