将多个php变量传递到索引页
更新 我是否可以使用以下代码替换$description中的$anagram,因为它将剥离what-is-an-anagram-of-listen.php,并且输出将是listen将多个php变量传递到索引页,php,variables,Php,Variables,更新 我是否可以使用以下代码替换$description中的$anagram,因为它将剥离what-is-an-anagram-of-listen.php,并且输出将是listen $anagram = str_replace('what-is-an-anagram-of-', ' ', pathinfo($file, PATHINFO_FILENAME)); 我有一个php脚本,它将列出某个文件夹的所有文件。 它获取页面标题,去掉破折号,并将其用作标题,然后获取页面描述并对其进行响应 &
$anagram = str_replace('what-is-an-anagram-of-', ' ', pathinfo($file, PATHINFO_FILENAME));
我有一个php脚本,它将列出某个文件夹的所有文件。
它获取页面标题,去掉破折号,并将其用作标题,然后获取页面描述并对其进行响应
<?php
if ($handle = opendir('../anagram/')) {
$fileTab = array();
preg_match("/name=\"description\" content=\"(.*?)\"/i", file_get_contents("../anagram/".$file), $matches);
$description = $matches[1];
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != 'index.php' && $file != 'error_log') {
$fileTab[] = $file;
}
}
closedir($handle);
shuffle($fileTab);
foreach($fileTab as $file) {
$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
$content = file_get_contents("../anagram/".$file);
if (!$content) {
echo "error reading file $file<br>";
}
else {
preg_match("/description = \"(.*?)\"/i", $content,$matches);
$description = $matches[1];
}
$buy .= '<div class="indexpage"><h6><a href="../anagram/'.$file.'">'.$title.'</a></h6><p>'.$description.'</p><p><a class="button-blue-short" href="../anagram/'.$file.'">Read More »</a></p></div>';
}
}
?>
<?=$buy?>
以下代码是文件夹中某个页面的示例../anagram/索引页面读取描述并使用它创建索引页面
<?php
$anagram = "listen";
$pagetitle = "What is an Anagram of $anagram";
$keywords = "Anagram of $anagram";
$description = "What is an Anagram of $anagram, an anagram is creating a word or phrase by moving around the letters of a different word or phrase, using all the original letters of $anagram what other words can be made from the word $anagram.";
include("../include/head.php");
?>
我的问题是在我的索引页上,我无法让php代码从$desription读取$anagram,它只是将其作为$anagram进行回显,但它应该说是listen。请参见:
根据您的设置和PHP版本,将变量置于双引号中应该会产生正确的输出,但您还有几个其他选项可用:
<?php
$anagram = "listen";
$test1 = "some text ${anagram} more text\n";
$test2 = "some text {$anagram} more text\n";
$test3 = "some text ".$anagram." more text\n";
echo $test1;
echo $test2;
echo $test3;
?>
应该都能正常工作。$content=file\u get\u contents../anagram/$file;-这里您只是将文件内容读入一个字符串,其中的PHP代码不会以这种方式执行。您是否尝试过使用$description=什么是$Anagram的一个拼字法。一个拼字法是通过移动不同单词或短语的字母来创建一个单词或短语,使用所有原始的$Anagram字母。从$anagram这个词还可以造出什么其他的词呢。;如果您想重新使用包含该脚本中变量名的代码,您需要使用include或require,就像稍后使用head.php一样。正如misorude所说,file_get_contents只是将文件的内容作为字符串加载,它不会将其视为可执行代码。@Ishpreet谢谢我尝试过,但它只是重复,什么是一个字谜,因为$description=什么是一个字谜,我不确定你的总体目标是什么,但您似乎正在尝试一种复杂的方法来实现半动态内容管理系统。为此,最好使用合适的CMS,或者至少创建一种数据库驱动的方式来加载数据,而不是像一个充满几乎相同的PHP脚本的整个文件夹。