我想分开html并使用echo来更轻松地进一步设计/编辑这个html片段,就像这样
<li>
<a href="<?php echo $filepath; ?>"><?php echo $file; ?></a>
</li>
这是我当前的代码,但我无法轻松编辑PHP代码中的html
<?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>';
}
}
}
?>
答案 0 :(得分:4)
我强烈建议PHP's alternative syntax进行模板化。例如
$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;
}