简体   繁体   中英

Using sed to replace <? with <?php

I'm trying to programmatically replace <? with <?php in a bunch of file, but my sed regex isn't behaving like I expected. Can you tell me what's wrong with it?

I'm testing it on the command line here:

$ sed -e 's/<\?/<\?php/g'
<?
<?php?<?php
d
<?phpd<?php

我不认为你需要逃脱吗?:

sed -e 's/<?/<?php/g'

You don't need to escape the back reference in the replacement.

sed 's#<\?#<?php#'

In a pipe, to correct for doubling the php:

sed 's#<\?#<?php#g' | sed 's#phpphp#php#g'

这也将跳过与<?=$variable匹配:

sed -e 's/<?\([^=p]\)/<?php\1/g' -e 's/<?$/<?php/g'

Do you really have to use sed, or can you use perl as well?

perl -pi.tmp -e 's,^<\?(?!php),<?php,' *.php *.inc
rm *.tmp

I am using a negative look-ahead to avoid generating <?phpphp in cases where the files already start with the correct characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM