0

Good morning to everyone here, attempt to replace a series of characters in different PHP files taking into account the following:

The files are lines like this:

if($_GET['x']){

And so I want to replace:

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

But we must take into account that there are files in lines like the following, but they do not want to modify the

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

I try as follows but I can not because I change all lines containing $ _GET ["x"]

My example:

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

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

1 Answer 1

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

The pattern above for if($_GET['x']){ would never match if($_GET["x"] == $_GET["x"]){.

Update:

This would change if($_GET['x']){ or if($_GET["x"]){ to if(isset($_GET['x'])){:

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

Another update:

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

Would change anything in the form of if($_GET['<something>']){ or if($_GET["<something>"]){.

Sign up to request clarification or add additional context in comments.

28 Comments

Thank you very much for your quick response, but this code still did not make any change :(
What's the output of sed --version? Any other error message you see?
sed (GNU sed) 4.2.2, not replace me $ _GET ['x'] for isset ($ _GET ['x']) ignoring lines that are like this $ _GET ['x'] == $ _GET ['x']
Mine is actually just 4.2.1. What do you get if you do echo "if(\$_GET['x']){" | sed -e "s|if *(\$_GET\['x'\]) *{|if(isset(\$_GET['x'])){|g"?
if(isset($_GET['x'])){ if(isset($_GET['x'])){
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.