1

I'm using this UDF for the PREG_REPLACE function in MySQL and everything seems to be working fine so far. However, my goal now is to find and encode entities inside pre tags in a column. So ultimately I'd like to get this:

<pre>
    <strong>Hello World!</strong>
</pre>

To look like this:

<pre>
    &lt;strong&gt;Hello World!&lt;/strong&gt;
</pre>

I'm using the PREG_REPLACE function to find the contents inside the pre tags like this:

SELECT PREG_REPLACE('/<pre>(.*?)<\\/pre>/sm', '\\1', '<pre><strong>Hello World</strong></pre>');

Now I'd like to replace \\1 with something that would say "replace with ENCODE_ENTITIES('\\1'). Obviously it could be any other function, like UPPER for example, but UPPER('\\1') doesn't give much. I kind of like the idea of PREG_REPLACE_EVAL in PHP's implementation of preg_replace which allows something like this:

preg_replace("/(<\/?)(\w+)([^>]*>)/e", 
    "'\\1'.strtoupper('\\2').'\\3'", 
    $html_body);

Any ideas on how to implement something similar in MySQL? Or maybe I'm heading the wrong way? Thanks!

4
  • 1
    Are you sure pre will not be nested with other pre elements?
    – FailedDev
    Commented Nov 2, 2011 at 10:07
  • @FailedDev yes I'm pretty sure, at least in my case, but you're right it's something we should watch out for. Thanks!
    – kovshenin
    Commented Nov 2, 2011 at 12:37
  • You can't watch out for it. If this is the case regex is not a good solution for this task. If you are 100% sure that pre elements would not contain other pre elements I could offer a solution to your current problem.
    – FailedDev
    Commented Nov 2, 2011 at 12:41
  • @FailedDev I'm certain, but note that I'm looking for a MySQL solution, not a Python script that would operate on an SQL dump ;)
    – kovshenin
    Commented Nov 2, 2011 at 14:03

1 Answer 1

1

Unless you want to modify the source code for the UDF to add that feature, there's no way to do that in MySQL. I suggest you pick your favorite programming language and write a program to make the update for you, or perhaps ensure that this modification is performed before the data hits the database.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.