Jump to content

[SOLVED] Using preg_match involving a "(" in variable


Damo2k

Recommended Posts

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.

 

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/dubbus.php?day=Mon+to+Fri&route=16a&from=From%20DUBLIN%20AIRPORT

(parsing http://www.dublinbus.ie/your_journey/viewer.asp?route=16a to show timetables to Dublin airport)

 

However for "From LOWER RATHFARNHAM (Nutgrove Avenue)" there is a problem.

http://damohere.freehostia.com/dubbus/dubbus.php?day=Mon+to+Fri&route=16a&from=From%20LOWER%20RATHFARNHAM%20%20(Nutgrove%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/dubbus.php?day=Mon+to+Fri&route=16a&from=From%20LOWER%20RATHFARNHAM%20%20(Nutgrove%20Avenue

you'll get:

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.