php回显文件url,并从结果中删除dash和.php
我有一个php脚本,它将从文件夹中回显文件列表,并在我的页面上随机显示它们 此时,它会显示文件的url,例如:what-can-cause-tooth-decage.php 问:有没有办法从结果中删除破折号和.php,以便显示: 什么会导致蛀牙而不是什么会导致蛀牙。phpphp回显文件url,并从结果中删除dash和.php,php,arrays,strip,Php,Arrays,Strip,我有一个php脚本,它将从文件夹中回显文件列表,并在我的页面上随机显示它们 此时,它会显示文件的url,例如:what-can-cause-tooth-decage.php 问:有没有办法从结果中删除破折号和.php,以便显示: 什么会导致蛀牙而不是什么会导致蛀牙。php <?php if ($handle = opendir('health')) { $fileTab = array(); while (false !== ($file = readdir($hand
<?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?>
谢谢
<?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?>
这就是你要找的吗
$str = 'what-can-cause-tooth-decay.php';
$str = str_replace('.php', '', str_replace('-', ' ', $str));
echo $str;
//what can cause tooth decay
这就是你要找的吗
$str = 'what-can-cause-tooth-decay.php';
$str = str_replace('.php', '', str_replace('-', ' ', $str));
echo $str;
//what can cause tooth decay
您可以尝试:
$string = 'what-can-cause-tooth-decay.php';
$rep = array('-','.php');
$res = str_replace($rep,' ', $string);
var_dump($res);
输出:
string 'what can cause tooth decay ' (length=27)
您可以尝试:
$string = 'what-can-cause-tooth-decay.php';
$rep = array('-','.php');
$res = str_replace($rep,' ', $string);
var_dump($res);
输出:
string 'what can cause tooth decay ' (length=27)
问题有两个方面:
$fileTab[] = strtr(pathinfo($file, PATHINFO_FILENAME), '-', ' ');
另见:
更新
从我收集的另一个答案中,您还希望随机选择一组10个文件来显示;下面的代码应该可以做到这一点:
foreach(array_slice($fileTab, 0, 10) as $file) {
问题有两个方面:
$fileTab[] = strtr(pathinfo($file, PATHINFO_FILENAME), '-', ' ');
另见:
更新
从我收集的另一个答案中,您还希望随机选择一组10个文件来显示;下面的代码应该可以做到这一点:
foreach(array_slice($fileTab, 0, 10) as $file) {
你有没有用“what is php.php”测试过这个问题?@Jack有趣的是,
被忽略了。修正了。你有没有用“what is php.php”测试过这个问题?@Jack发现
被忽略了。修复。你很好,谢谢-你知道如何让它在页面加载时只显示10个文件,而不是健康文件夹中的所有119个文件吗?-1。正则表达式在这里是多余的,尤其是像这样的链式。正则表达式是多余的,是的。但我认为这里的表现不会有问题。我的解决方案是在长度不超过30或50个字符的简单文件名上使用preg_replace。IDK,根据输入的大小,我认为我们看不出strtr或str_replace与此之间有多大区别。非常感谢-你知道如何让它在页面加载时只显示10个文件,而不是显示健康文件夹中的所有119个文件吗?-1。正则表达式在这里是多余的,尤其是像这样的链式。正则表达式是多余的,是的。但我认为这里的表现不会有问题。我的解决方案是在长度不超过30或50个字符的简单文件名上使用preg_replace。IDK,根据输入的大小,我不认为我们会看到STRTR或StruxRead和this.thanks jack之间有很大的不同,但有一件事我没有想到的是,它在没有DASH和.php的情况下回响结果,但也会从链接中删除它们,所以点击它们就到没有找到的页面。oops@user1649416好吧,您可以编写“$fileTab[$file]=”而不是“$fileTab[$file]=”,然后在foreachHello中使用数组键,对不起,您可以为一个php傻瓜再次解释一下。将$file添加到此处$fileTab[$file]=strtr(pathinfo($file,pathinfo_FILENAME),'-','';然后?@user1649416然后foreach$fileTab作为$origFileName=>$shortFileName)对不起,但是你能确定我应该在哪里添加我已经更新了我的代码的代码吗?谢谢杰克,但是有一件事我没想到的是它在没有DASH和.php的情况下回响了结果,但是也把它们从链接中删除了,所以点击它们就到没有找到的页面。oops@user1649416那么,您可以编写“$fileTab[$file],而不是“$fileTab[]=”='然后使用foreachHello中的数组键,很抱歉,您可以为php假人再次解释一下。将$file添加到此处$fileTab[$file]=strtr(pathinfo($file,pathinfo_FILENAME),'-','';然后?@user1649416然后foreach$fileTab作为$origFileName=>$shortFileName){
对不起,你能指出我应该在哪里添加那一行吗?我已经更新了上面的代码