Jump to content

Advanced Regular Expressions


Greywacke
 Share

Recommended Posts

hi all,i need to match a regular expression searching a string for the following example, regardless of indentation and result name as well as well as sql strings:the top level sql is defined as an array now, and joined when echoing at the end of the xml page.

			$result0 = mysql_query($sql0);			$err = "ERROR #".mysql_errno().": ".mysql_error()." on Line ".(__LINE__-1);			if (mysql_errno()> 0) {				$GLOBALS["sql"] .= $sql0;				$GLOBALS["sql"] .= strtoupper($err)."\n";			}

the indentation, mysql result and sql string names vary across the source code.i'm currently redeveloping the error trapping to a page level array, with the following function (i need to be notified of empty result sets too):

$sql = array();function mysql_query_errors($sql, $conn, $doc, $line) {	$result = mysql_query($sql,$conn);	if (mysql_num_rows($result) < 1 || mysql_errno($conn) > 0) {		if (mysql_num_rows($result) < 1) {			$err = "ERROR #0000: An empty result set was returned by the query defined in ".$doc." on line ".$line--;		} elseif (mysql_errno($conn) > 0) {			$err = "ERROR #".mysql_errno($conn).": ".mysql_error($conn)." in ".$doc." on Line ".$line;		}		array_push($GLOBALS["sql"],$sql,$err);	}	return $result;}echo "	<sql>".xmlentities(join("\n",$sql))."</sql>\n"; // echoed at end of page

the ajax alerts errors only if <sql></sql> contains text.i've managed to come up with the following sofar:

[\t ]*\$\w+ = mysql_query(.+);[\n\r.]+\}

i need to go and replace all the instances of the code above with the following:

$result = mysql_query_errors($tsql, $conn, __FILE__, __LINE__);

with the relevant indentation and result name as well as well as sql strings.however it does not match in Dreamweaver CS4 (which has wonderful "as you type" javascript syntax handling ^^)... i've just about run out of ideas, heres the reference to Regular Expressions in Dreamweaver CS4.i've placed it on the forum where the code is the most relevant.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

after rethinking, i've come up with the following to match that code that needs replacing, but it still won't match, wether the }'s are escaped or not:

[\t =]mysql_query([\W\w\s]);[\n.^}]}

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

rethought again, and it still refuses to match 0o

\$[\t =]mysql_query([\$\w, ]);[\n.]\}

Link to comment
Share on other sites

Wait, are you trying to match the PHP code?!?!How about using token_get_all() to actually parse the code, find the kind of token you're after, and do whatever you want with it.If the idea is replacement, you can write out all tokens except the matched one, replace the matched one, and write the rest out.

Link to comment
Share on other sites

rethought again (probably about the 18th time), and it still refuses to match 0oworks :)

([\t ]+)([\$\w]+)( = mysql_query)\(([^\)]+)[^\}]+[\}]

replacing it with the following, to implement the error handling function:

$1$2$3_errors\($4, $conn, __FILE__, __LINE__\);

this issue is RESOLVED!!!

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

wow lol, it had to be improved to use replace all:find:

([\t ]+)([\$\w]+)( = mysql_query)\(([\$\w]+)[^\},]+[\}]

replace:

$1$2$3_errors\($4,$conn,__FILE__,__LINE__\);

i believe it is truly complete now :)

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

okay another error has surfaced,unrelated to the topic but related to the error handling, will add url to this reply when done.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...