I'm trying to use a regular expression to match a hidden html tag and
replace it with the results of a mysql query. The query is based off
a part of the hidden tag. For example:
The article looks like this ("Math" is the value I want to pass to my
function call, it could be any value for the department name):
$article = "Below is a listing of the staff in our Math
department:<p>< !-- Directory:Math -->";
I'm using this preg_replace to insert the revised article text:
$article = preg_replace('/<!-- Directory:([^]*)
-->/',phoneBook($db ,"'\\1'"),$arti cle);
The phoneBook function uses the backreference value of "Math" when
forming the query:
function phoneBook($db,$ department) {
$sql = "select * from staffdirectory where deptName='$depa rtment'";
print "$sql";
mysql_query($sq l,$db);
// other processing here to return an html table of staff members
}
This function will print:
"select * from staffdirectory where deptName='Math' "
.... however, the query fails, returning 0 results. If I change the
preg_replace to:
$article = preg_replace('/<!-- Directory:([^]*)
-->/',phoneBook($db ,"Math"),$artic le);
.... the sql prints out exactly the same, but now the query works fine
and returns all staff members from the Math department. What could be
causing this problem? I'm assuming that a backreference in
preg_replace is not treated as a string for some reason and therefore
changes the typecast of the $sql variable which is why mysql_query
doesn't work. I've seen other examples of people using functions like
strtoupper('\\1 ') to return the backreference in all upper case, but
this doesn't even work with my expression above. Help!
Brian
replace it with the results of a mysql query. The query is based off
a part of the hidden tag. For example:
The article looks like this ("Math" is the value I want to pass to my
function call, it could be any value for the department name):
$article = "Below is a listing of the staff in our Math
department:<p>< !-- Directory:Math -->";
I'm using this preg_replace to insert the revised article text:
$article = preg_replace('/<!-- Directory:([^]*)
-->/',phoneBook($db ,"'\\1'"),$arti cle);
The phoneBook function uses the backreference value of "Math" when
forming the query:
function phoneBook($db,$ department) {
$sql = "select * from staffdirectory where deptName='$depa rtment'";
print "$sql";
mysql_query($sq l,$db);
// other processing here to return an html table of staff members
}
This function will print:
"select * from staffdirectory where deptName='Math' "
.... however, the query fails, returning 0 results. If I change the
preg_replace to:
$article = preg_replace('/<!-- Directory:([^]*)
-->/',phoneBook($db ,"Math"),$artic le);
.... the sql prints out exactly the same, but now the query works fine
and returns all staff members from the Math department. What could be
causing this problem? I'm assuming that a backreference in
preg_replace is not treated as a string for some reason and therefore
changes the typecast of the $sql variable which is why mysql_query
doesn't work. I've seen other examples of people using functions like
strtoupper('\\1 ') to return the backreference in all upper case, but
this doesn't even work with my expression above. Help!
Brian
Comment