title: PHP regex to replace SQL Query
body: <p>I have an strange situation where i receive in a function by parameter an XML where a field in particular is a SQL Query. The problem i detected is that i want to modify the sql query on the fly to add some extra things:</p>
  
  <p>What i receive for example is:</p>
  
  <pre><code>    &lt;p n="KeyFields"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;p n="Funciones"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;/q&gt;&lt;q i="" t="RequestQuery"&gt;&lt;p n="TipoQuery"&gt;&lt;![CDATA[ExecuteSQL]]&gt;&lt;/p&gt;&lt;p n="TipoEntidad"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;p n="Datos"&gt;&lt;![CDATA[INSERT INTO ps_module_group 
  SELECT 
    m.id_module, 1 AS id_shop, g.id_group 
  FROM ps_module m 
    JOIN ps_group g 
  WHERE NOT EXISTS 
    ( 
    SELECT 1 FROM ps_module_group 
    WHERE id_module=m.id_module AND id_group=g.id_group 
    ) 
  ]]&gt;&lt;/p&gt;&lt;p n="KeyFields"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;p n="Funciones"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;/q&gt;&lt;/qq&gt;
  </code></pre>
  
  <p>The idea is to modify the query with this extra:</p>
  
  <pre><code>AND m.id_module != 122
  </code></pre>
  
  <p>So it looks like this:</p>
  
  <pre><code>    &lt;p n="KeyFields"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;p n="Funciones"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;/q&gt;&lt;q i="" t="RequestQuery"&gt;&lt;p n="TipoQuery"&gt;&lt;![CDATA[ExecuteSQL]]&gt;&lt;/p&gt;&lt;p n="TipoEntidad"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;p n="Datos"&gt;&lt;![CDATA[INSERT INTO ps_module_group 
  SELECT 
    m.id_module, 1 AS id_shop, g.id_group 
  FROM ps_module m 
    JOIN ps_group g 
  WHERE NOT EXISTS 
    ( 
    SELECT 1 FROM ps_module_group 
    WHERE id_module=m.id_module AND id_group=g.id_group 
    ) AND m.id_module != 122
  ]]&gt;&lt;/p&gt;&lt;p n="KeyFields"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;p n="Funciones"&gt;&lt;![CDATA[]]&gt;&lt;/p&gt;&lt;/q&gt;&lt;/qq&gt;
  </code></pre>
  
  <p>I'm 100% sure that is not good to do this in the server side but query is generated outside my control...
  My problem is that i need to validate the whole query in the regex as other queries could be passed and don't need to be modified, just this exact query. My biggest problem and this is why i'm not able to do it is because of the multiline query that i'm not sure how to enter that in a regex, even more due to tabs, spaces, etc.
  I'm using PHP in the server side.</p>
  
  <p>Is there any option?
  Thanks in advance.</p>