Damo2k Posted August 20, 2008 Share Posted August 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/120504-solved-using-preg_match-involving-a-quotquot-in-variable/ Share on other sites More sharing options...
Damo2k Posted August 20, 2008 Author Share Posted August 20, 2008 I solved the problem with preg_quote() Quote Link to comment https://forums.phpfreaks.com/topic/120504-solved-using-preg_match-involving-a-quotquot-in-variable/#findComment-621053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.