You are pretty ambiguous with your question. From what I can gleem, the problem is that you have some sort of query string, in which values got interpolated before being escaped. This is most easily fixable by properly escaping the value BEFORE it is inserted into the string. Of course, this wouldn't be a problem if you are using Perl and the DBI module; however, you are not, so use whatever solution PHP has available (there is some sort of add_slashes function, I can't quite remember the name; look it up on php.net).

If you have no control over the values, then you have a much harder problem. Here is a way to fix it with Perl, although I'm not sure how much this will help you:

$_ = "INSERT INTO TABLENAME VALUES ('bunch o'f text','another text','a +not'cool'her text')"; s/\G (?<=') ((?: (?> [^\\']* ) | \\. | '(?![,)]) )*) ('[,)]) /fix("$1").$2/egx; print; sub fix { $_[0]=~s~(?<!\\)'~\\'~g;$_[0] }

Of course, this won't even work for every case. If you have a string like:

INSERT INTO TABLENAME VALUES ('bunch o',f text','another text')

or

INSERT INTO TABLENAME VALUES ('bunch o')f text','another text')

Then this solution will break, and I honestly can't think of a way around it (at least off the top of my head). The best solution will be to try to fix the values BEFORE they are inserted into the query string.


In reply to Re: Impossible RegEx? by jryan
in thread Impossible RegEx? by DaWolf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.