Author Topic: SQL Query Regex  (Read 1055 times)

0 Members and 1 Guest are viewing this topic.

Offline SuchyTopic starter

  • Enthusiast
  • Posts: 137
    • View Profile
SQL Query Regex
« on: March 26, 2010, 02:29:25 PM »
I'm having troubles with a regular expressions.

What I want is to replace the begining of a SQL query.
ex.
Code: [Select]
SELECT * FROM employes ...
SELECT name.employes, id.employes FROM employes ...
SELECT name.employes, id.dept, nr.room FROM ...
....

I need to replace anything between SELECT and FROM with
Code: [Select]
SELECT COUNT(*) AS num_of_rows  FROM


<?php
	
	
 echo 
"old query -> " $query "<br><br>";
	
	
 
	
	
 
$regex "/^(SELECT|Select|select)([\s]|[a-zA-Z0-9\.\*-_,]|[\s])+[(FROM|From|from)]/";
	
	
 
	
	
 if (
preg_match($regex $query ))
	
	
 
	
echo 
"YES<br>";
	
	
else
	
	
	
echo 
"NO<br>"
	
;
	
	
 
	
	
 
$query preg_replace($regex"SELECT COUNT(*) AS num_of_rows  FROM "$query);

	
	
echo 
"new query -> " $query "<br><br>";
?>



How can I modify my regex inorder for this to work ?


Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: SQL Query Regex
« Reply #1 on: March 26, 2010, 07:14:25 PM »
You seem to be over complicating the subject somewhat, is there some reason something like this won't work?

$output preg_replace('#^select .* from#i''SELECT COUNT(*) AS num_of_rows  FROM'$input);
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline SuchyTopic starter

  • Enthusiast
  • Posts: 137
    • View Profile
Re: SQL Query Regex
« Reply #2 on: March 29, 2010, 12:51:40 PM »
Thats it, Thanks !

But the problem that I had was with newlines and returns.

Ex. To keep the query looking clean I had something like this
Code: [Select]
SELECT name.employes, birhtdate.employes,
             building.room,  nr.room,
             name.dept, manager.dept,  id.dept,
            ...
FROM ...
Instead of writing it in a single line.

This is how I solved the problem

<?php
$query 
str_replace("\r" "" $query);
$query str_replace("\n" "" $query);
$query str_replace("\t" "" $query);
$regex "/^SELECT .* FROM/i";

// $regex = "/^SELECT (.|\n|\s|\t|\r)* FROM/i";              <--- this did not work for me
?>



Once again, thanks for the shorter regex.

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: SQL Query Regex
« Reply #3 on: March 29, 2010, 12:53:55 PM »
If you add the s modifier it will solve the problem of newlines without needing the replacements beforehand.

$output preg_replace('#^select .* from#is''SELECT COUNT(*) AS num_of_rows  FROM'$input);
« Last Edit: March 29, 2010, 12:54:30 PM by cags »
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline SuchyTopic starter

  • Enthusiast
  • Posts: 137
    • View Profile
Re: SQL Query Regex
« Reply #4 on: March 29, 2010, 01:15:04 PM »
Does not work for me, but thanks for the hint !

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: SQL Query Regex
« Reply #5 on: March 29, 2010, 01:59:40 PM »
The s modifier makes the . match newline characters, so I can't see how it wouldn't work. But nevermind, if you have it working you have it working.
CodeCanyon - Cheap, High Quality, Ready Made Scripts.