PHP脚本-注释/取消注释行
谁能给我一些在文件中切换注释的想法或解决方案? 读取值并在值所在行中切换注释/取消注释 例如,我想包括类模型并初始化它 在某些文件中,有准备好的包含和初始化:PHP脚本-注释/取消注释行,php,string,comments,Php,String,Comments,谁能给我一些在文件中切换注释的想法或解决方案? 读取值并在值所在行中切换注释/取消注释 例如,我想包括类模型并初始化它 在某些文件中,有准备好的包含和初始化: //include('foo/Model.php'); //$model = new Model(); 对于那些无法理解问题所在的人来说,功能是必要的 如何取消注释?感谢您为您的问题添加更多见解!事实上,这是一个非常有趣的故事 据我所知,您正在寻找一种动态方式来注释/取消注释文件中的行 因此,让我们首先定义参数: 我们想要操作一个特定
//include('foo/Model.php');
//$model = new Model();
对于那些无法理解问题所在的人来说,功能是必要的
如何取消注释?感谢您为您的问题添加更多见解!事实上,这是一个非常有趣的故事 据我所知,您正在寻找一种动态方式来注释/取消注释文件中的行 因此,让我们首先定义参数:
- 我们想要操作一个特定的文件(我们需要文件名)
- 我们希望在此文件中切换特定行号(行号列表)
file()
的简便函数
file():返回数组中的文件。数组的每个元素都对应于文件中的一行,换行符仍然附着
牢记这一点,我们拥有编写函数所需的一切:
<?php
function file_toggler(string $file, array $lineNumbers)
{
// normalize because file() starts with 0 and
// a regular user would use (1) as the first number
$lineNumbers = array_map(function ($number) {
return $number - 1;
}, $lineNumbers);
// get all lines and trim them because
// file() keeps newlines inside each line
$lines = array_map('trim', file($file));
// now we can take the line numbers and
// check if it starts with a comment.
foreach ($lineNumbers as $lineNumber) {
$line = trim($lines[$lineNumber]);
if (substr($line, 0, 2) == '//') {
$line = substr($line, 2);
} else {
$line = '//' . $line;
}
// replace the lines with the toggled value
$lines[$lineNumber] = $line;
}
// finally we write the lines to the file
// I'm using implode() which will append a "\n" to every
// entry inside the array and return it as a string.
file_put_contents($file, implode(PHP_EOL, $lines));
}
toggleFileComments(__DIR__ . '/file1.php', [3, 4]);
感谢您为您的问题添加更多见解!事实上,这是一个非常有趣的故事
据我所知,您正在寻找一种动态方式来注释/取消注释文件中的行
因此,让我们首先定义参数:
- 我们想要操作一个特定的文件(我们需要文件名)
- 我们希望在此文件中切换特定行号(行号列表)
记住这一点,我们需要读取一个文件并将它们的行分割成行号。PHP为此提供了一个名为file()
的简便函数
file():返回数组中的文件。数组的每个元素都对应于文件中的一行,换行符仍然附着
牢记这一点,我们拥有编写函数所需的一切:
<?php
function file_toggler(string $file, array $lineNumbers)
{
// normalize because file() starts with 0 and
// a regular user would use (1) as the first number
$lineNumbers = array_map(function ($number) {
return $number - 1;
}, $lineNumbers);
// get all lines and trim them because
// file() keeps newlines inside each line
$lines = array_map('trim', file($file));
// now we can take the line numbers and
// check if it starts with a comment.
foreach ($lineNumbers as $lineNumber) {
$line = trim($lines[$lineNumber]);
if (substr($line, 0, 2) == '//') {
$line = substr($line, 2);
} else {
$line = '//' . $line;
}
// replace the lines with the toggled value
$lines[$lineNumber] = $line;
}
// finally we write the lines to the file
// I'm using implode() which will append a "\n" to every
// entry inside the array and return it as a string.
file_put_contents($file, implode(PHP_EOL, $lines));
}
toggleFileComments(__DIR__ . '/file1.php', [3, 4]);
删除此/
?先阅读,然后评论。我需要一个从文件中删除注释的函数。您打算在项目中作为一次性更改还是作为通过UI控制的定期任务来执行?使用条件?Christoph,我想反复使用它。这是关于启用、禁用应用程序选项。删除此/
?先阅读,然后评论。我需要一个从文件中删除注释的函数。您打算在项目中作为一次性更改还是作为通过UI控制的定期任务来执行?使用条件?Christoph,我想反复使用它。这是关于启用和禁用应用程序选项的。如果($option){include,new;}
,那么简单的有什么错呢?@u_mulder你完全正确。这也许是他需要解决的老年退休金计划任务的更好方法,但老年退休金计划问题的答案是不同的。他希望在文件中切换注释;)如果($option){include,new;}
,那么simple有什么错呢?@u\u mulder你完全正确。这也许是他需要解决的老年退休金计划任务的更好方法,但老年退休金计划问题的答案是不同的。他希望在文件中切换注释;)