?
Solved

Get matchs with regex

Posted on 2009-04-15
7
Medium Priority
?
277 Views
Last Modified: 2012-05-06
Hello all, I want to get the table names from a query, example below:

SELECT t1.`id`, t1.`title`, t2.`title` AS parent, t1.`description`, t1.`published`
FROM `mng_troubleshooter` AS t1
LEFT JOIN `mng_troubleshooter` AS t2 ON t1.parent=t2.id
ORDER BY t2.`title`, t1.`title`

I need regex where I put t1 and will get `mng_troubleshooter`, I know that I would get it by using the "AS t1" but can't get the correct regex for it :(

Thanks in advance!
0
Comment
Question by:componentslab
  • 4
  • 3
7 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24152767
To clarify, do you want to swap all occurrences of t1 with `mng_troubleshooter`?


$string = 'SELECT t1.`id`, t1.`title`, t2.`title` AS parent, t1.`description`, t1.`published`
FROM `mng_troubleshooter` AS t1
LEFT JOIN `mng_troubleshooter` AS t2 ON t1.parent=t2.id
ORDER BY t2.`title`, t1.`title`';
 
$pattern = '/t1/';
 
$replacement = '`mng_troubleshooter`';
 
$new_string = preg_replace($pattern, $replacement, $string);
 
print $new_string;
 
############################################################
#Result is:
SELECT `mng_troubleshooter`.`id`, `mng_troubleshooter`.`title`, t2.`title` AS parent, `mng_troubleshooter`.`description`, `mng_troubleshooter`.`published`
FROM `mng_troubleshooter` AS `mng_troubleshooter`
LEFT JOIN `mng_troubleshooter` AS t2 ON `mng_troubleshooter`.parent=t2.id
ORDER BY t2.`title`, `mng_troubleshooter`.`title`

Open in new window

0
 

Author Comment

by:componentslab
ID: 24152961
Nope, what I want is to get from there is mng_troubleshooter.

From the example query I posted, I would put in the regex t1 and the result would be mng_troubleshooter.

SELECT t1.`id`, t1.`title`, t2.`title` AS parent, t1.`description`, t1.`published`
FROM `mng_troubleshooter` AS t1
LEFT JOIN `mng_troubleshooter2` AS t2 ON t1.parent=t2.id
ORDER BY t2.`title`, t1.`title`
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24153241
Something like this then?
$string = 'SELECT t1.`id`, t1.`title`, t2.`title` AS parent, t1.`description`, t1.`published`
FROM `mng_troubleshooter` AS t1
LEFT JOIN `mng_troubleshooter2` AS t2 ON t1.parent=t2.id
ORDER BY t2.`title`, t1.`title`';
 
$aliasToFindTableFor = "t1";
$pattern = '/`?(\w+)`?\sas\s`?'.$aliasToFindTableFor.'`?/i';
$count = preg_match($pattern, $string, $matches);
 
$result = $matches[1];
print "Result: $result";

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Terry Woods
ID: 24153244
ps: That's gotta be worth more than 50 points!
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 200 total points
ID: 24153275
Actually, this version will be slightly more robust (it handles multiple spaces):
$string = 'SELECT t1.`id`, t1.`title`, t2.`title` AS parent, t1.`description`, t1.`published`
FROM `mng_troubleshooter` AS t1
LEFT JOIN `mng_troubleshooter2` AS t2 ON t1.parent=t2.id
ORDER BY t2.`title`, t1.`title`';
 
$aliasToFindTableFor = "t1";
$pattern = '/\s+`?(\w+)`?\s+as\s+`?'.$aliasToFindTableFor.'`?/i';
$count = preg_match($pattern, $string, $matches);
 
$result = $matches[1];
print "Result: ".htmlentities($result);
  

Open in new window

0
 

Author Comment

by:componentslab
ID: 24155989
I agree with the points :)
0
 

Author Closing Comment

by:componentslab
ID: 31570599
Worked like a charm, thanks a lot.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question