Page 1 of 1

named subpattern preg_match problem

Posted: Wed Apr 25, 2012 7:50 am
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.

Re: named subpattern preg_match problem

Posted: Wed Apr 25, 2012 8:29 am
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.

Re: named subpattern preg_match problem

Posted: Wed Apr 25, 2012 10:00 am
by AGISB
Thanks that seems to work. I will test this if it gets all CR and Stuff later.