named subpattern preg_match problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

named subpattern preg_match problem

Post by AGISB »

I got following format input

Code: Select all

$plain = "INSERT INTO storyentries (p_id, e_id, c_id, e_imp, e_email, e_title, e_text, e_url, e_create, IP ) VALUES ('2', '420', '11977','1','test@test.com','Test Title','Normal text with CR and extra chars','http://www.test.com',NOW(),'127.0.0.0' )";
now I am trying to use preg_match with named subpatterns on that with limited success.

Code: Select all

preg_match("/^INSERT INTO entries \(p_id, e_id, c_id, e_imp, e_email, e_title, e_text, e_url, e_create, IP \) VALUES \('(?P<page_id>\d+)', '(?P<entry_id>\d+)', '(?P<ortid>\d+)','(?P<entry_importance>\d+)'/", $plain, $treffer);
works fine on the numbers that can be extracted but now I get problems with the text fields, that can basically be off any char CR etc.

I am not able to get the text stuff working at all.

Any help would be appreaciated.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: named subpattern preg_match problem

Post by requinix »

Keep going with the same pattern you have now but use

Code: Select all

([^'\\\\]*|\\\\.)*"
for the string contents. That will alternate between (1) anything that isn't an apostrophe (for the string quotes) or a backslash (for escaped characters) and (2) a backslash and its escaped character.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Re: named subpattern preg_match problem

Post by AGISB »

Thanks that seems to work. I will test this if it gets all CR and Stuff later.
Post Reply