-1

I have very old php4-5 project that needs to migrate to php7.2

It has a lot of codes like:

echo '<img src="img/'.$r[imgurl1].'"
strip_tags($r[details])

It's now in my local ubuntu 18 apache&php-server. How can i search and replace all the .php files to add quotes ? I cannot add doublequotes if [] contains $ (Variable) sign in it, of course And also if it already contains " or '

I know little bit of preg, but not enough.. I have sublime text3 and notepad++ that can do also replaces if only now the right regular expression. Pleas help!

gray_15
  • 411
  • 1
  • 7
  • 13

2 Answers2

1

Using Notepad++

  • Ctrl+H
  • Find what: \$\w+\[\K([^$"'\]]+)
  • Replace with: "$1"
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

\$              # $ sign
\w+             # 1 or more word characters
\[              # opening square bracket
\K              # forget all we have seen until this position
(               # start group 1
    [^$"'\]]+   # 1 or more any character that is not $, ", ', ]
)               # end group

Given:

echo '<img src="img/'.$r[imgurl1].'"
strip_tags($r[details])
$abc = $def[$xxx]

Result for given example:

echo '<img src="img/'.$r["imgurl1"].'"
strip_tags($r["details"])
$abc = $def[$xxx]

Using php

$in = <<<'EOD'
echo '<img src="img/'.$r[imgurl1].'"
strip_tags($r[details])
$abc = $def[$xxx]
EOD;
$res = preg_replace('/\$\w+\[\K([^$"\'\]]+)/', '"$1"', $in);
echo $res;

Output:

echo '<img src="img/'.$r["imgurl1"].'"
strip_tags($r["details"])
$abc = $def[$xxx]
Toto
  • 89,455
  • 62
  • 89
  • 125
0

yes, this helped thanks! It also matches to $array[12], meaning numbers get quoted too. Found out from irc that this was working solution:

\[(\w*[A-Za-z]\w*)\]

substitution: ["\1"]
gray_15
  • 411
  • 1
  • 7
  • 13