Using a regex backreference in a mysql query – PHP

  php

Q(Question):

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’"),$article);

The phoneBook function uses the backreference value of "Math" when
forming the query:
function phoneBook($db,$department) {
$sql = "select * from staffdirectory where deptName=’$department’";
print "$sql";
mysql_query($sql,$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"),$article);

…. 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

A(Answer):

Brian Richmond wrote:

I’m using this preg_replace to insert the revised article text:
$article = preg_replace(‘/<!– Directory:([^]*) –>/’,phoneBook($db,"’\\1’"),$article);

ERROR!!! ___________________________________^^^^^^^^^^

The [ start a character class composed of anything BUT (because of the ^)
the following characters up to the end of the class (the next ]) which
never comes!

Turn on all errors, warnings, and notices for your scripts by including
this line at the very top of them:

<?php error_reporting(E_ALL); ini_set(‘display_errors’, ‘1’); ?>

–= my mail box only accepts =–
–= Content-Type: text/plain =–
–= Size below 10001 bytes =–

A(Answer):

On 2004-03-14, Brian Richmond <we*******@4minds.com> wrote:

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’"),$article);

<!– Directory:(.*?) –>

Perhaps that you still have to escape <, >, – or ! (Thats your job 😉
function phoneBook($db,$department) {
$sql = "select * from staffdirectory where deptName=’$department’";
print "$sql";
mysql_query($sql,$db);
// other processing here to return an html table of staff members
}

With the code given, it appears as if you are returning nothing (void)

$article = preg_replace(‘/<!– Directory:([^]*)
–>/’,phoneBook($db,"Math"),$article);

Thus phoneBook($db,"Math") returns nothing.


http://home.mysth.be/~timvw

A(Answer):

Ok, my lack of experience with regular expressions shows in my
example. Given what I’m trying to do, what would be the best
expression for getting the right value out in a format that I can use
in my sql query?

Pedro Graca <he****@hotpop.com> wrote in message news:<c3*************@ID-203069.news.uni-berlin.de>…

The [ start a character class composed of anything BUT (because of the ^)
the following characters up to the end of the class (the next ]) which
never comes!

A(Answer):

Brian Richmond wrote:

Pedro Graca <he****@hotpop.com> wrote in message news:<c3*************@ID-203069.news.uni-berlin.de>…

The [ start a character class composed of anything BUT (because of the ^)
the following characters up to the end of the class (the next ]) which
never comes!

Ok, my lack of experience with regular expressions shows in my
example. Given what I’m trying to do, what would be the best
expression for getting the right value out in a format that I can use
in my sql query?

You’re on the right track 🙂
Do turn on error_reporting in your scripts!

$article = preg_replace(‘/<!– Directory:(\w+) –>/e’,
## _______________________________^^^______^_
## \w+ one or more ‘word’ characters
## /e option to have PHP evaluate the replacement
phoneBook($db, ‘\\1’), $article);
## don’t forget that the phoneBook() function must return something and
## not merely print it.

USENET would be a better place if everybody read: ** mail address **
http://www.catb.org/~esr/faqs/smart-questions.html ** is valid for **
http://www.netmeister.org/news/learn2quote2.html ** "text/plain" **
http://www.expita.com/nomime.html ** to 10K bytes **

A(Answer):

Either I’m completely crazy or you need to use preg_replace_callback() for
what you’re trying to do.

Uzytkownik "Brian Richmond" <we*******@4minds.com> napisal w wiadomosci
news:1e**************************@posting.google.c om…

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’"),$article);

The phoneBook function uses the backreference value of "Math" when
forming the query:
function phoneBook($db,$department) {
$sql = "select * from staffdirectory where deptName=’$department’";
print "$sql";
mysql_query($sql,$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"),$article);

… 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

A(Answer):

Assume for now that phoneBook return something, which is does but the
details were left off of this post. (It returns a string containing
HTML code for a table of staff members with phone numbers and email
addresses).

The hidden tag is supposed to allow me to pass the part after
"Directory:" into the function call to phoneBook(). Once inside the
function, I can print the $sql variable and it shows up correctly.
When the query runs, it does not select any entries, but it also
doesn’t give me any mysql errors.

So using phoneBook($db,"\\1") doesn’t work, but phoneBook($db,"Math")
does, even if both set the $sql statement correctly. I’ve also tried
manipulating the value of $department once inside the phoneBook
function. It’s like the backreference is not passed as a string at
all. I can’t get substrings of it, change the case to upper or lower
or anything. Does this clarify any more?
Tim Van Wassenhove <eu**@pi.be> wrote in message news:<c3*************@ID-188825.news.uni-berlin.de>…

<!– Directory:(.*?) –>

Perhaps that you still have to escape <, >, – or ! (Thats your job 😉
With the code given, it appears as if you are returning nothing (void)
Thus phoneBook($db,"Math") returns nothing.

LEAVE A COMMENT