?PHP;批处理脚本以&lt取代PHP短开放标记 [英] Batch script to replace PHP short open tags with <?php
问题描述
我收集了大量的书面多年来PHP文件,我需要正确地更换所有的短开的标记插入适当的明确公开的标记。
更改&LT;?进入&LT; PHP的
我觉得这个常规的前pression会正确选择它们:
<?pre>&LT; \\(\\ S | \\ n | \\ T | [^ A-ZA-Z])
这需要照顾的情况下像
&LT; //
&LT; / *?
但我不知道如何处理整个文件夹树并检测PHP文件扩展名,应用常规的前pression并保存文件时,它已经改变了。
我有感觉,这可能是pretty简单,如果你掌握了正确的工具。 (还有就是sed的手册中一个有趣的黑客:的 4.3例/重命名文件,以小写)。
也许我错了。结果
或者,也许这可能是一个oneliner?
不使用正则表达式解析形式语言 - 你总是碰上你没有预料到的干草堆。这样的:
&LT ;?
$ BLA ='&GT?;怎么办? &LT;?';
它的使用更安全知道该语言的结构的处理器。对于HTML,这将是一个XML处理器;对于PHP,内置标记生成器扩展程序。它具有 T_OPEN_TAG
解析器令牌,该令牌&LT匹配; ?PHP
,&LT;
或&LT;%
和 T_OPEN_TAG_WITH_ECHO
,它匹配&LT; =
或&LT;%=
。替换所有短的开放式标签,你会发现所有这些标记并替换 T_OPEN_TAG
与&LT; PHP
和 T_OPEN_TAG_WITH_ECHO
与&LT;?PHP的回声
的实施就留给读者做练习:)
修改1 :驯兽师是这么样<一个href=\"http://stackoverflow.com/questions/684587/batch-script-to-replace-php-short-open-tags-with-php/1647429#1647429\">provide 之一。
编辑2 :与 系统short_open_tag的值为< ?
,<$ C / code> 在的php.ini
,&LT关闭$ C>&LT;%
和&LT; =
不会被替代脚本的认可。使这些系统上的脚本的工作,使 short_open_tag的值为
通过命令行选项:
PHP -d short_open_tag的值为=开short_open_tag_replacement_script.php
P.S。 为token_get_all()手册页和googleing为的标记生成器的* token_get_all *创意组合,和解析器标记名称可能的帮助。
p.p.s。又见<一个href=\"http://stackoverflow.com/questions/645862/regex-to-parse-define-contents-possible/645957\">Regex解析定义()的内容,可能吗?的这里SO
I have a large collection of php files written over the years and I need to properly replace all the short open tags into proper explicit open tags.
change "<?" into "<?php"
I think this regular expression will properly select them :
<\?(\s|\n|\t|[^a-zA-Z])
which takes care of cases like
<?//
<?/*
but I am not sure how to process a whole folder tree and detect the .php file extension and apply the regular expression and save the file after it has been changed.
I have the feeling this can be pretty straightforward if you master the right tools. (There is an interesting hack in the sed manual: 4.3 Example/Rename files to lower case).
Maybe I'm wrong.
Or maybe this could be a oneliner?
don't use regexps for parsing formal languages - you'll always run into haystacks you did not anticipate. like:
<?
$bla = '?> now what? <?';
it's safer to use a processor that knows about the structure of the language. for html, that would be a xml processor; for php, the built-in tokenizer extension. it has the T_OPEN_TAG
parser token, which matches <?php
, <?
or <%
, and T_OPEN_TAG_WITH_ECHO
, which matches <?=
or <%=
. to replace all short open tags, you find all these tokens and replace T_OPEN_TAG
with <?php
and T_OPEN_TAG_WITH_ECHO
with <?php echo
.
the implementation is left as an exercise for the reader :)
EDIT 1: ringmaster was so kind to provide one.
EDIT 2: on systems with short_open_tag
turned off in php.ini
, <?
, <%
, and <?=
won't be recognized by a replacement script. to make the script work on such systems, enable short_open_tag
via command line option:
php -d short_open_tag=On short_open_tag_replacement_script.php
p.s. the man page for token_get_all() and googleing for creative combinations of tokenizer, *token_get_all*, and the parser token names might help.
p.p.s. see also Regex to parse define() contents, possible? here on SO
这篇关于?PHP;批处理脚本以&lt取代PHP短开放标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!