【问题标题】:PHP Formatter for single-line code blocks用于单行代码块的 PHP 格式化程序
【发布时间】:2016-01-19 14:29:12
【问题描述】:

我正在尝试配置 Eclipse PHP 格式化程序,如果它们之间只有 1 行代码,则将打开和关闭 php 标记保留在 1 行上(同时如果有更多代码行,则保留默认的换行格式)。

例子:

<td class="main"><?php
echo drm_draw_input_field('fax') . '&nbsp;' . (drm_not_null(ENTRY_FAX_NUMBER_TEXT) ? '<span class="inputRequirement">' . ENTRY_FAX_NUMBER_TEXT . '</span>' : '');
?></td>

应格式化为:

<td class="main"><?php echo drm_draw_input_field('fax') . '&nbsp;' . (drm_not_null(ENTRY_FAX_NUMBER_TEXT) ? '<span class="inputRequirement">' . ENTRY_FAX_NUMBER_TEXT . '</span>': ''); ?></td>

有什么方法可以用 Eclipse 实现这一点吗?还是其他建议/格式化程序?

编辑: Eclipse 似乎没有这样的格式化选项,如下面的 cmets 所述。任何现有的替代品可以做到这一点?

【问题讨论】:

  • 据我所知,你不能在eclipse 中做这样的事情,因为eclipse 只有格式化代码部分的选项,我的意思是 php 标签内的文本部分 &lt;?php ...code text... ?&gt;,但我可以建议为替代创建简单的 php 脚本,它将读取给定的 php 文件内容格式并覆盖它
  • 是的,您似乎对Eclipse 格式选项是正确的。问题还在于寻求可以做到这一点的现有替代方案。如果你知道,请发布它:)

标签: php eclipse formatting coding-style


【解决方案1】:

正如我在问题评论“As i know you can't do such thing in eclipse as eclipse has only options to format code part i mean the text part inside php tags &lt;?php ...code text... ?&gt;”下已经提到的那样

但是你可以用这个 php 脚本来实现它

开始前非常重要:备份你将在dirToArray()函数中提到的php项目

// Recursive function to read directory and sub directories
// it build based on php's scandir - http://php.net/manual/en/function.scandir.php
function dirToArray($dir) {

    $result = array();      
    $cdir = scandir($dir);

    foreach ($cdir as $key => $value){
        if (!in_array($value,array(".",".."))){
            if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){
                $result = array_merge(
                       $result, 
                       dirToArray($dir . DIRECTORY_SEPARATOR . $value) 
                );
            }else{
                $result[] = $dir . DIRECTORY_SEPARATOR . $value;
            }
        }
    }

    return $result;
}

// Scanning project files
$files = dirToArray("/home/project"); // or C:/project... for windows

// Reading and converting to single line php blocks which contain 3 or less lines
foreach ($files as $file){

    // Reading file content
    $content = file_get_contents($file);

    // RegExp will return 2 arrays 
    // first will contain all php code with php tags
    // second one will contain only php code
    // UPDATED based on Michael's provided regexp in this answer comments
    preg_match_all( '/<\?php\s*\r?\n?(.*?)\r?\n?\s*\?>/i', $content, $blocks );

    $codeWithTags = $blocks[0];
    $code = $blocks[1];

    // Loop over matches and formatting code
    foreach ($codeWithTags as $k => $block){
        $content = str_replace($block, '<?php '.trim($code[$k]).' ?>', $content );
    }

    // Overwriting file content with formatted one
    file_put_contents($file, $content);
}

注意: 这只是一个简单的例子,当然这个脚本可以改进

// Result will be that this
text text text<?php 
   echo "11111"; 
?>
text text text<?php 
   echo "22222"; ?>
text text text<?php echo "33333"; 
?>
<?php
   echo "44444";
   echo "44444";
?>

// will be formated to this
text text text<?php echo "11111"; ?>
text text text<?php echo "22222"; ?>
text text text<?php echo "33333"; ?>
<?php
   echo "44444";
   echo "44444";
?>

【讨论】:

  • 看起来不错!如果您可以改进您的代码以直接读取目录并且只使用带有 3 个换行符的块,请编辑您的答案。它将使它成为赢家! :)
  • 这个正则表达式匹配所有在线用户。应该让你的代码更简单:regex101.com/r/jV7rG2/2&lt;\?php\s*\n(.*?)\n\s*\?&gt;
  • 感谢@Michael,我根据您的建议更新了正则表达式
  • @AnnieTrubak 我添加了目录扫描部分
  • @Armen 并非如此。只有在线的 PHP 块才能匹配。检查我发给你的正则表达式——更新为\rregex101.com/r/jV7rG2/3
【解决方案2】:

在sublimetext中,手动命令是ctrl + J

自动地,我建议你看看这个:

https://github.com/heroheman/Singleline

【讨论】:

    猜你喜欢
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2020-11-14
    相关资源
    最近更新 更多