简体   繁体   English

如何在页面顶部赋予.php文件唯一名称,并在index.php文件上显示唯一名称

[英]How to give a .php file unique name at top of page, and display unique name on index.php file

I have a question and it's probably a simple one. 我有一个问题,可能是一个简单的问题。 What I would like to do is be able to place a unique name at the top of each .php file in a folder in the php code area. 我想做的是能够在php代码区域的文件夹中的每个.php文件的顶部放置一个唯一的名称。 I would then like a script on the index.php file to look in that folder, pull out the unique names of each .php file found at the top of each page in the php code, and display them in a list on the index.php page. 然后,我希望index.php文件上的脚本在该文件夹中查找,拉出在php代码每一页顶部找到的每个.php文件的唯一名称,并将它们显示在索引的列表中。 php页面。

Would I have to do something like this at the top of the page: 我是否必须在页面顶部执行以下操作:

< ?
{{{{{uniquenamehere}}}}}
? >

And If so, what would the code look like for grabbing uniquenamehere and displaying in on the index.php page? 如果是的话,在这里获取唯一名称并显示在index.php页面上的代码将是什么样?

Thanks in advance, let me know if I need to be any more clear in my question. 预先感谢,让我知道我是否需要更清楚地说明问题。 Sorry if it's a really simple question, I'm stumped! 抱歉,如果这是一个非常简单的问题,我很困惑!

EDIT 编辑

Getting this warning when using answer below: Warning: file_get_contents(test.php) [function.file-get-contents]: failed to open stream: No such file or directory in /path/index.php 使用以下答案时收到此警告:警告:file_get_contents(test.php)[function.file-get-contents]:无法打开流:/path/index.php中没有此类文件或目录

Here's the code I am using, 这是我正在使用的代码,

  <?php

// Scan directory for files
$dir = "path/";
$files = scandir($dir);

// Iterate through the list of files 
foreach($files as $file)
{
// Determine info about the file
$parts = pathinfo($file);

// If the file extension == php
if ( $parts['extension'] === "php" )
{
// Read the contents of the file
$contents = file_get_contents($file);

// Find first occurrence of opening template tag
$from = strpos($contents, "{{{{{");

// Find first occurrence of ending template tag
$to = strpos($contents,"}}}}}");

// Pull out the unique name from between the template tags
$uniqueName = substr($contents, $from+5, $to);

// Print out the unique name
echo $uniqueName ."<br/>";
}
}
?>

Not tested, but it should be roughly something like this. 未经测试,但应该大致是这样的。

<?php

// Scan directory for files
$fileInfo = pathinfo(__FILE__);

$dir = $fileInfo['dirname'];
$files = scandir($dir);

// Iterate through the list of files
foreach($files as file)
{
  // Determine info about the file
  $parts = pathinfo($file);

  // If the file extension == php
  if ( $parts['extension'] == "php" )
  {
    // Read the contents of the file
    $contents = file_get_contents($file);

    // Find first occurrenceof opening template tag
    $from = strpos($contents, "{{{{{");

    // Find first occurrenceof ending template tag
    $to = strpos($contents,"}}}}}");

    // Pull out the unique name from between the template tags
    $uniqueName = substr($contents, $from+5, $to);

    // Print out the unique name
    echo $uniqueName ."<br/>";
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM