I have a php script that will echo a list of files from a folder and display them randomly on my page.
At the moment it displays the url of the file for example: what-can-cause-tooth-decay.php
Qusetion: is there a way to remove the dashes – and .php from the results so that it would diplay:
what can cause tooth decay Instead of what-can-cause-tooth-decay.php
<?php
if ($handle = opendir('health')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileTab[] = $file;
}
}
closedir($handle);
shuffle($fileTab);
foreach($fileTab as $file) {
$thelist .= '<p><a href="../health/'.$file.'">'.$file.'</a></p>';
}
}
?>
<?=$thelist?>
Thanks
<?php
if ($handle = opendir('health')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileTab[$file] = strtr(pathinfo($file, PATHINFO_FILENAME), '-', ' ');
}
}
closedir($handle);
shuffle($fileTab);
foreach(array_slice($fileTab, 0, 10) as $file) {
$thelist .= '<p><a href="../health/'.$file.'">'.$file.'</a></p>';
}
}
?>
<?=$thelist?>
The problem is two-fold:
The below should work just fine for you:
See also:
strtr()pathinfo()Update
From another answer I’ve gathered that you additionally wish to select a random set of 10 files to show; the below code should do just that: