2016-01-19 14 views
12

Ich versuche, die Eclipse-PHP-Formatierer zu konfigurieren, dass die Öffnung zu halten und zu schließen php Tags auf 1 Zeile, wenn es nur 1 Zeile Code zwischen ihnen ist (während die Standard-Zeilen Formatierung zu halten, wenn Es gibt mehr Codezeilen.PHP Formatter für einzeilige Codeblocks

Beispiel:

<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> 

zu formatiert werden soll:

<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> 

Gibt es eine Möglichkeit, dies mit Eclipse zu erreichen? Oder ein anderer Vorschlag/Formatierer?

EDIT: Es scheint, Eclipse hat keine solche Formatierungsoption, wie in den Kommentaren unten erläutert. Bestehende Alternativen, die das können?

+0

Wie ich weiß, Sie nicht so etwas in 'eclipse' tun kann als' eclipse' nur Optionen hat Codeteil zu formatieren i bedeuten, den Textteil innerhalb php-Tags '', aber ich kann als alternative Erstellung von einfachen pHP-Skript vorschlagen, die es pHP-Datei Content-Format angegeben werden gelesen und überschrieben werden es – Armen

+0

Ja, es scheint, dass Sie Recht haben mit 'Eclipse'-Formatierungsoptionen. Die Frage verlangt auch nach einer bestehenden Alternative, die das tun kann. Wenn Sie einen kennen, posten Sie es :) – CreativeMind

Antwort

4

Wie ich bereits unter Frage Kommentar erwähnt „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 <?php ...code text... ?>

Aber man kann es mit diesem PHP-Skript

Sehr wichtig vor dem Start erreichen: Sichern Sie Ihr PHP-Projekt, das Sie in dirToArray() erwähnen gehen Funktion

// 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); 
} 

HINWEIS:Dies ist nur eine einfache Prüfung ple und natürlich kann dieses Skript

// 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"; 
?> 
+1

Sieht gut aus! Wenn Sie Ihren Code verbessern können, um Verzeichnisse direkt zu lesen und nur Blöcke mit 3 Zeilenumbrüchen zu verwenden, bearbeiten Sie Ihre Antwort. Es wird ein Gewinner sein! :) –

+1

Dieser regexp passt auf alle Onliner. Sollte Ihr Code viel einfacher machen: https://regex101.com/r/jV7rG2/2 '' – Michael

+0

Thanks @ Michael i aktualisiert Regexp basierend auf Ihrem Vorschlag – Armen