用 <?php 替换 PHP 短打开标签的批处理脚本

Batch script to replace PHP short open tags with lt;?php(用 lt;?php 替换 PHP 短打开标签的批处理脚本)
本文介绍了用 <?php 替换 PHP 短打开标签的批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有大量多年来编写的 php 文件,我需要将所有短的开放标签正确替换为适当的显式开放标签.

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|
|	|[^a-zA-Z])

处理像

<?//
<?/*

但我不确定如何处理整个文件夹树并检测 .php 文件扩展名并应用正则表达式并在更改后保存文件.

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.

我觉得如果您掌握了正确的工具,这会非常简单.(sed 手册中有一个有趣的 hack:4.3 示例/将文件重命名为小写).

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? <?';

使用了解语言结构的处理器更安全.对于 html,那将是一个 xml 处理器;对于 php,内置 tokenizer 扩展.它有 T_OPEN_TAG 解析器令牌,它匹配 <%T_OPEN_TAG_WITH_ECHO,匹配 <%=.要替换所有短的开放标签,您需要找到所有这些标记并将 T_OPEN_TAG 替换为 <?php 并将 T_OPEN_TAG_WITH_ECHO 替换为 .

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

编辑 1:ringmaster 对 提供一个.

EDIT 1: ringmaster was so kind to provide one.

EDIT 2:在带有 short_open_tagphp.ini<% 中关闭 不会被替换脚本识别.要使脚本在此类系统上运行,请通过命令行选项启用 short_open_tag:

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

附言token_get_all() 的手册页 和谷歌搜索 tokenizertoken_get_all 和解析器令牌名称可能会有所帮助.

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.另见正则表达式解析define()内容,可能吗?所以

这篇关于用 &lt;?php 替换 PHP 短打开标签的批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)