用于单行代码块的PHP格式化程序
我正在尝试配置EclipsePHP格式化程序,如果打开和关闭用于单行代码块的PHP格式化程序,php,eclipse,formatting,coding-style,Php,Eclipse,Formatting,Coding Style,我正在尝试配置EclipsePHP格式化程序,如果打开和关闭PHP标记之间只有一行代码,则将它们保持在一行上(如果有更多代码行,则保持默认的新行格式) 例如: <td class="main"><?php echo drm_draw_input_field('fax') . ' ' . (drm_not_null(ENTRY_FAX_NUMBER_TEXT) ? '<span class="inputRequirement">' . ENTRY_F
PHP
标记之间只有一行代码,则将它们保持在一行上(如果有更多代码行,则保持默认的新行格式)
例如:
<td class="main"><?php
echo drm_draw_input_field('fax') . ' ' . (drm_not_null(ENTRY_FAX_NUMBER_TEXT) ? '<span class="inputRequirement">' . ENTRY_FAX_NUMBER_TEXT . '</span>' : '');
?></td>
正如我在问题注释“中已经提到的,我知道你不能在eclipse中做这样的事情,因为eclipse只有设置代码部分格式的选项,我指的是php标记中的文本部分
”
但是您可以使用这个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);
}
//读取目录和子目录的递归函数
//它基于php的scandir-http://php.net/manual/en/function.scandir.php
函数dirToArray($dir){
$result=array();
$cdir=scandir($dir);
foreach($cdir作为$key=>$value){
if(!in_数组($value,数组(“.”,“.”)){
if(is_dir($dir.DIRECTORY_SEPARATOR.$value)){
$result=array\u merge(
$result,
dirToArray($dir.DIRECTORY\u SEPARATOR.$value)
);
}否则{
$result[]=$dir.dir.DIRECTORY\u SEPARATOR.$value;
}
}
}
返回$result;
}
//扫描项目文件
$files=dirToArray(“/home/project”);//或C:/project。。。窗户
//读取并转换为包含3行或更少行的单行php块
foreach($files作为$file){
//读取文件内容
$content=file\u get\u contents($file);
//RegExp将返回2个数组
//第一个将包含所有带有php标记的php代码
//第二个将只包含php代码
//根据Michael在此回答评论中提供的regexp更新
preg_match_all('//i',$content,$blocks);
$codeWithTags=$blocks[0];
$code=$blocks[1];
//循环匹配和格式化代码
foreach($codeWithTags为$k=>$block){
$content=str_replace($block,,$content);
}
//用格式化文件覆盖文件内容
文件内容($file,$content);
}
注意:这只是一个简单的示例,当然这个脚本可以改进
//结果将是
文本文本文本
文本文本文本
文本文本文本
//将为此进行格式化
文本文本文本
文本文本文本
文本文本文本
在SubmiteText中,手动命令为ctrl+J
从自动角度来看,我建议您看一下:
我知道你不能在eclipse
中做这样的事情,因为eclipse
只有设置代码部分格式的选项,我指的是php标签
中的文本部分,但我可以建议你创建一个简单的php脚本,它将读取给定的php文件内容,并将其格式化并覆盖是的,关于Eclipse
格式选项,您似乎是对的。问题还在于需要一个现有的替代方案来实现这一点。如果你知道一个,请张贴:)看起来不错!如果你可以改进你的代码,直接读取目录,只取3个换行的块,请编辑你的答案。这将使它成为赢家!:)这个regexp匹配所有的onliner。应该使您的代码更简单:
谢谢@Michael我根据您的suggestion@AnnieTrubak我添加了目录扫描part@Armen事实并非如此。只有联机PHP块才会匹配。检查我发送给您的用\r
// 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";
?>