Maybe explaining the real intention to all this work some PCRE master could help me.
I'm trying to do a library that could extract data information from a table depending on some of their fields values. But all information is in a SQL dump file. In other words I need to construct a file SQL system (which do not suport joins and does exactly pattern match).
eg.:
Suppose a file named 'DATABASE1.table' with the above contents:
.
.
.
INSERT INTO Users VALUES (39, 'Raphael');
CREATE TABLE Tabela1 (
cod_item int not null primary key,
name char[100] not null,
title char[100] not null,
message char[2000] not null
);
INSERT INTO Tabela1 VALUES (1,'Andressa','My opinions','I didn\\'t receive any message.');
INSERT INTO Tabela1 VALUES (2,'The One','Recommended sites','www.google.com');
INSERT INTO Tabela1 VALUES (6,'William','Books','The Blind Watchmaker');
INSERT INTO Tabela1 VALUES (9,'Ernesto','Music','Pain of Salvation');
INSERT INTO Tabela1 VALUES (20,'John','John\\'s Agenda','My Agenda: 8 o\\'clock - sth');
CREATE TABLE Games (
.
.
.
Suppose now a function called MAGIC which receives the table, wanted field names and a where clause as input. MAGIC returns all respective fields values. This functions use PHP 'eval' function to evaluate the where clause.
Input format:
$info['TABLE_NAME']['FIELDS'] = array('FIELD_NAME_A', 'FIELD_NAME_C', ...);
$info['TABLE_NAME']['WHERE'] = "FIELD_NAME_B = 'some value' and FIELD_...";
Eg.:
<?php
$info['Tabela1']['FIELDS'] = array('cod_item', 'title');
$info['Tabela1']['WHERE'] = "message = 'I didn\\\'t receive any message.'";
$x = $MAGIC($info);
print_r($x);
// $x['cod_item']: 1
// $x['title']: 'My opinions'
?>
I'd like to retrieve all information from a row which message field value is, for example, 'I didn\\'t receive any message.'. It seems very simple, but it isn't. MAGIC musn't be fooled by strings (enclosed in single quotes) having the field name. Also it musn't be fooled by escaped single quotes.
FIELD_NAME : [COLOR=blue]message[/COLOR]
FIELD_VALUE: [COLOR=red]'I didn\\'t receive any [/COLOR][COLOR=blue]message[/COLOR][COLOR=red].'[/COLOR]
How I could do this in PHP? For each matched INSERT do:
<?php
.
.
preg_match_all("/'(?:(?:(?:\\\\\\\\\\)*\\\\\\')|(?:[^']))+'/", $where, $matches);
$blocks = $matches[0];
$pat = array("/'(?:(?:(?:\\\\\\\\\\)*\\\\\\')|(?:[^']))+'/",
"/=/",
"/message/");
$repl = array("##WEIRD:PATT:WEIRD##",
"==",
"'I didn\\'t receive any message.'");
$tmp = preg_replace($pat, $repl, $where);
foreach($blocos as $curblock)
$tmp = preg_replace("##WEIRD:PATT:WEIRD##", $curblock, $tmp, 1);
echo($tmp); // "'I didn\\'t receive any message.' == 'I didn\\'t receive any message.'"
eval("\$test = (\$tmp);\n");
if ($test)
.
.
.
?>
But if the specified table has too many rows MAGIC would be very slow. So, I ask again: is there a RegExpression which replaces all php code?
Thx.