将 PHP 转换为控制结构的替代语法


Converting PHP to the alternative syntax for control structures

有没有一种有效的方法可以将PHP模板从正常的控制结构语法自动转换为替代语法?

从标准语法

<?php if($a == 5) { ?>
    A is equal to 5
<?php } ?>

到替代语法

<?php if ($a == 5): ?>
    A is equal to 5
<?php endif; ?>

显然包括所有其他控制结构。

如果你在谈论代码本身,那么你可以使用你的IDE,ctrl+f, ctrl+r在jetbrains中,或者制作宏,甚至制作自己的模板生成器,但所有这些都不是自动的对接

我创建了一个小工具来解决这个问题。

cd ~/tools
git clone https://github.com/oranfry/php-syntax-converter.git
# show converted file on screen
cd ~/my-project
cat myfile.php | ~/tools/php-syntax-converter/to-alternative.php
# convert all .php files in the current directory:
cd ~/my-project
find -name '*.php' | while read file; do cat "$file" | ~/tools/php-syntax-converter/to-alternative.php > /tmp/converted.php && mv /tmp/converted.php "$file"; done