問題描述
我有大量多年來編寫的 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_tag
在 php.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() 的手冊頁 和谷歌搜索 tokenizer、token_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()內容,可能嗎?所以
這篇關于用 <?php 替換 PHP 短打開標簽的批處理腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!