Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

sed - Batch script to replace PHP short open tags with <?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])

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...