Using preg_match involving a "(" in variable
Posted: Wed Aug 20, 2008 4:57 am
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.
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:
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?
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";
}
}
}
}
}
}
}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 117I 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?