473,651 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
6 4655
Brian Richmond wrote:
I'm using this preg_replace to insert the revised article text:
$article = preg_replace('/<!-- Directory:([^]*) -->/',phoneBook($db ,"'\\1'"),$arti cle);

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 =--
Jul 17 '05 #2
On 2004-03-14, Brian Richmond <we*******@4min ds.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'"),$arti cle);

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

Perhaps that you still have to escape <, >, - or ! (Thats your job ;)
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
}
With the code given, it appears as if you are returning nothing (void)

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


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

--
http://home.mysth.be/~timvw
Jul 17 '05 #3
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!

Jul 17 '05 #4
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 **
Jul 17 '05 #5
Either I'm completely crazy or you need to use preg_replace_ca llback() for
what you're trying to do.

Uzytkownik "Brian Richmond" <we*******@4min ds.com> napisal w wiadomosci
news:1e******** *************** ***@posting.goo gle.com...
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

Jul 17 '05 #6
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.

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1737
by: Chuck Haeberle | last post by:
I have an interesting regular expression challenge for someone more experienced with them than I for a data layer class... I need an expression to search a SQL statement (any type, SELECT INSERT UPDATE OR DELETE) and find all single apostrophes which should be replaced with a double apostrophe without affecting the apostrophes used to delimit the string values: Example: SELECT field FROM table WHERE stringfield = 'This is one of...
0
1290
by: rodrigo | last post by:
This is the source text I am working on. 3593 blue $15.95 3944 yellow 8.10 4001 brown $9.75 Basically I want to extract each part separately like this part 1 3593
6
396
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
3
2692
by: Vidar Skjelanger | last post by:
I have a regex for matching VB6-functions, but it hangs on one specific function. The regex: ^(Public\s)?(Declare\s|Static\s)?(Function)\s(\S)+\(((Optional\s)?(ByVal\s|ByRef\s)?(ParamArray\s)?(\S)+(\sAs\s(\S)+)?(\s=\s(\S)+)?(\,\s)?)*\)(\sAs\s(\S)+)? The string on which it hangs: "Public Function FlaggAsOccupiedEx2(Pid As Long, UserName As String, Workstation As String, Optional IsReadOnly As Boolean = False,
2
273
by: choyk1 | last post by:
I want to match some elements with regular expression ------------------------------------------------------------------------------- <word> <type.> <meaning> : <synonyms> e.g. preeminent adj. having paramount rank, dignity, or importance : OUTSTANDING ------------------------------------------------------------------------------- But, I have no idea about the pattern. string pattern =...
7
1766
by: Edgardo Rossetto | last post by:
Hi, I have something like: string result = null; try { string exp = @"(?'GroupName'<title>.*\(({4})\).*</title>)"; Regex r = new Regex(exp); result = r.Match(html).Groups.Value; return result.Trim();
1
2892
by: darrel | last post by:
I have some vb.net code that is running a regex, matching groups, and replacing them. I'm trying to come up with a simple script that will strip all attributes from all HTML tags. This is what I have: ============================================================= function stripAllAttributes(ByVal textToParse as String, ByVal tagToFind as String) as String
14
3975
by: mens libertina | last post by:
Disclaimer: In addition to reading the skimpy entries at php.net, I've searched this group and alt.php to no avail. Basically, I'm trying to use a template to send email reminders, but I can't use my backreferences the way I would like. (template.inc is not installed on my host.) If I'm approaching the problem wrong, please have mercy and tell me how to solve it! :) I have an array, $info, that looks like this (this is just one user):...
0
1159
by: amrnablus | last post by:
hello everyone, i'm trying to do something weird in boost regex, i need to get characters "repeated times backreference" !! i.e "3 abc" , "10 abcdefghij" .... any ideas ? ps: i tried {\\2} but it did not work !! :(
0
8802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7297
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.