用shell脚本替换PHP文件中的字符串

时间:2014-06-11 16:04:01

标签: linux shell

早上好,这里的每个人都会尝试更换不同PHP文件中的一系列字符,并考虑到以下因素:

文件是这样的行:

if($_GET['x']){

所以我想替换:

if(isset($_GET['x'])){

但我们必须考虑到以下行中的文件,但他们不想修改

if($_GET["x"] == $_GET["x"]){

我尝试如下但我不能,因为我更改了包含$ _GET ["x"]

的所有行

我的例子:

find . -name "*.php" -type f -exec ./code.sh {} \;

sed -i 's/\ if($_GET['x']){/ if(isset($_GET['x'])){/' "$1"

1 个答案:

答案 0 :(得分:0)

find . -name "*.php" -type f -print0 | xargs -0 sed -i -e "s|if *(\$_GET\['x'\]) *{|if(isset(\$_GET['x'])){|g" --

if($_GET['x']){的上述模式永远不会匹配if($_GET["x"] == $_GET["x"]){

更新

这会将if($_GET['x']){if($_GET["x"]){更改为if(isset($_GET['x'])){

find . -name "*.php" -type f -print0 | xargs -0 sed -i -e "s|if *(\$_GET\[[\"']x[\"']\]) *{|if(isset(\$_GET['x'])){|g" --

另一个更新:

find . -name "*.php" -type f -print0 | xargs -0 sed -i -e "s|if *(\$_GET\[[\"']\([^\"']\+\)[\"']\]) *{|if(isset(\$_GET['\1'])){|g" --

会以if($_GET['<something>']){if($_GET["<something>"]){

的形式更改任何内容
相关问题