Reputation: 428
I would like to separate the html and just use echo for easier further styling/editing this html snippet, like this
<li>
<a href="<?php echo $filepath; ?>"><?php echo $file; ?></a>
</li>
Here is my current code that works, but I can not easily edit the html inside the PHP code
<?php
$dir = ".";
$exclude = array( ".","..",".*","*.php" );
if (is_dir($dir)) {
$files = scandir($dir);
foreach($files as $file){
if(!in_array($file,$exclude)){
$filepath = str_replace(' ', '%20', $file);
echo '<li><a href=' . $dir . '/' . $filepath . '>' . $file . '</a></li>';
}
}
}
?>
Upvotes: 0
Views: 1370
Reputation: 164766
I highly recommend PHP's alternative syntax for templating. For example
$dir = ".";
$exclude = array( ".","..",".*","*.php" );
if (is_dir($dir)) {
$files = array_diff(scandir($dir), $exclude);
foreach ($files as $file) : ?>
<li>
<a href="<?= $dir ?>/<?= urlencode($file) ?>">
<?= $file ?>
</a>
</li>
<?php endforeach;
}
Upvotes: 4