Page 1 of 1

Using preg_match involving a "(" in variable

Posted: Wed Aug 20, 2008 4:57 am
by Damo2k
Hi, this is my first post here...
My problem.. using preg_match involving a "(" or ")" in a variable seems to be messing up the function on me.

I have tried escaping the "(" using preg_replace but was unsuccessful. As the data in the variable is dynamic, I cannot hardcode a "\" before the "(".

Ok some background info, My script parses a bus time table of a website based on what parameters its given.

Code: Select all

if ( (($_GET["day"]) != "") && (($_GET["route"]) != "") && (($_GET["from"]) != "") ) {
    $url = "http://www.dublinbus.ie/your_journey/viewer.asp?placeName=Your+Location&SelectedRoute=" . urlencode($_GET["route"]);
    
    $from = $_GET["from"];
    $day = $_GET["day"];
 
    $handle = @fopen("$url", "rb");
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            
            #TO DO, sort out strings containing (, )
            if (preg_match("/$from/", $buffer)) {     # <--PROBLEM HERE
                do {                    
                    $buffer = fgets($handle, 4096);
                } while (!preg_match("/<!-- $day -->/", $buffer));
                        
                if (preg_match("/<!-- $day -->/", $buffer)) {          
            
                    while(!preg_match("/<!-- end of table in $day/", $buffer)) {
                        $buffer = fgets($handle, 4096);
                        
                        #print space - legend char
                        if (preg_match("/<\/tr>/", $buffer)) {
                            print " ";
                        }
                        $tmp = strip_tags($buffer);
                        $tmp = trim($tmp);
                        if ($tmp != "") {                       
                            print "$tmp";                   
                        }
                    }   
                }
            }
        }
    }       
}
Now my website:

http://damohere.freehostia.com/dubbus/d ... %20AIRPORT
(parsing http://www.dublinbus.ie/your_journey/vi ... ?route=16a to show timetables to Dublin airport)

However for "From LOWER RATHFARNHAM (Nutgrove Avenue)" there is a problem.
http://damohere.freehostia.com/dubbus/d ... %20Avenue)returns nothing as the preg_match function does not get a match. If you remove the ")" at the end of the url and request it: http://damohere.freehostia.com/dubbus/d ... e%20Avenue
you'll get:

Code: Select all

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 40 in /home/www/damohere.freehostia.com/dubbus/dubbus.php on line 117
 
Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 40 in /home/www/damohere.freehostia.com/dubbus/dubbus.php on line 117
indicating that the brackets in the url are being interpreted in the regular expression. How will I get around this?

I tried using \( and \) in the url but php seems to be automatically escaping the \'s and not the (, )'s
I tried using preg_replace to try and replace a ( with a \( and it was unsuccessful for me.
I tried to encode the url. i.e. make the brackets %28 or %29 and still no joy.

I have ran out of idea's, can anyone help me?

Re: Using preg_match involving a "(" in variable

Posted: Wed Aug 20, 2008 7:38 am
by Damo2k
I solved the problem with preg_quote()

Re: Using preg_match involving a "(" in variable

Posted: Thu Aug 21, 2008 7:28 am
by GeertDD
Damo2k wrote:I solved the problem with preg_quote()
It must have been quite an eye-opener to find out about the preg_quote() function, wasn't it? :wink:

Re: Using preg_match involving a "(" in variable

Posted: Fri Aug 22, 2008 7:30 am
by Damo2k
yeah, im new to this :-)