title: Substitute Obfuscated PHP strings in Sed
body: <p>I'm writing a script to deobfuscate PHP code. And mostly it's done;the only thing left is to decode the strings within the code. So my input file could be something like:</p>
  
  <pre><code>    &lt;?php
       $myString="\x48\x45\x4c\x4c\x4f"; 
       $myOtherString="\x57\x4F\x52\x4C\x44";
      ?&gt;
  </code></pre>
  
  <p>Now, in my Script I have a loop over all the strings to replace them one by one. Therefore I have (as part of the loop) the following code ($S is declared as the obfuscated String inside the loop - just in case you were wondering where $S comes from):</p>
  
  <pre><code>    $S='"\x48\x45\x4c\x4c\x4f"'
      REALSTRING=`php -r 'eval("echo ".$argv[1].";");' $S`
      ESCAPEDREALSTRING=`printf "%q" "$REALSTRING"`
      ESCAPEDSOURCE=`printf "%q" "$S"`
      CODE4=`echo $CODE4 | sed "s/$ESCAPEDSOURCE/$ESCAPEDREALSTRING/g"`
  </code></pre>
  
  <p>I have searched the web for a solution and the printf step was recommended to escape the string correctly. Some of the strings in the real text are even RegExes, so maybe Sed is not suitable for the job. Does anybody have an idea?</p>
  
  <p>This attempt is using sed, but perl, awk or any other tool is also fine, as long as available in Bash per default on a standard Linux/MacOS system.</p>
  
  <p>As an example pair:<br>
  <code>"\x5c\50\x22\133\x30\55\x39\101\x2d\132\x61\55\x7a\134\x2b\5‌​7\x3d\135\x2a\42\x5c‌​\51"</code><br>
  should be converted to<br>
  <code>"\("[0-9A-Za-z\+/=]*"\)"</code><br>
  This is one of the possibly occurring RegEx I mentioned.  </p>
  
  <p>Best regards
  Merlin</p>