Using a regex backreference in a mysql query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Richmond

    Using a regex backreference in a mysql query

    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
  • Pedro Graca

    #2
    Re: Using a regex backreference in a mysql query

    Brian Richmond wrote:[color=blue]
    > I'm using this preg_replace to insert the revised article text:
    > $article = preg_replace('/<!-- Directory:([^]*) -->/',phoneBook($db ,"'\\1'"),$arti cle);[/color]
    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('displa y_errors', '1'); ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Tim Van Wassenhove

      #3
      Re: Using a regex backreference in a mysql query

      On 2004-03-14, Brian Richmond <webmaster@4min ds.com> wrote:[color=blue]
      > 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);[/color]


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

      Perhaps that you still have to escape <, >, - or ! (Thats your job ;)
      [color=blue]
      > 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
      > }[/color]

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

      [color=blue]
      > $article = preg_replace('/<!-- Directory:([^]*)
      > -->/',phoneBook($db ,"Math"),$artic le);[/color]

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

      --

      Comment

      • Brian Richmond

        #4
        Re: Using a regex backreference in a mysql query

        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 <hexkid@hotpop. com> wrote in message news:<c324r0$22 vms6$1@ID-203069.news.uni-berlin.de>...
        [color=blue]
        > 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!
        >[/color]

        Comment

        • Pedro Graca

          #5
          Re: Using a regex backreference in a mysql query

          Brian Richmond wrote:[color=blue]
          > Pedro Graca <hexkid@hotpop. com> wrote in message news:<c324r0$22 vms6$1@ID-203069.news.uni-berlin.de>...
          >[color=green]
          >> 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!
          >>[/color]
          > 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?[/color]

          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 **

          Comment

          • Chung Leong

            #6
            Re: Using a regex backreference in a mysql query

            Either I'm completely crazy or you need to use preg_replace_ca llback() for
            what you're trying to do.

            Uzytkownik "Brian Richmond" <webmaster@4min ds.com> napisal w wiadomosci
            news:1e550e8a.0 403140842.620ec f98@posting.goo gle.com...[color=blue]
            > 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[/color]


            Comment

            • Brian Richmond

              #7
              Re: Using a regex backreference in a mysql query

              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 <euki@pi.be> wrote in message news:<c32p16$22 btf1$1@ID-188825.news.uni-berlin.de>...
              [color=blue]
              >
              > <!-- Directory:(.*?) -->
              >
              > Perhaps that you still have to escape <, >, - or ! (Thats your job ;)
              >[/color]
              [color=blue]
              > With the code given, it appears as if you are returning nothing (void)
              >
              >
              > Thus phoneBook($db," Math") returns nothing.[/color]

              Comment

              Working...