Regex, PHP: assigning matches to an array after inserting expanded words -


regex, php: assigning matches array after inserting expanded words

in system matches user input against regex pattern allow pattern contain "concept words" marked twiddle (~). e.g. can define

~service-type '"oil change" rotation brake "tune up"' ,  ~day 'monday tuesday wednesday thursday friday' 

i can have pre-regex like:

.*get.*~service-type.*~day 

which preprocessing gets expanded to:

/.*get.*(oil change|rotation|brake|tune up).*(monday|tuesday|wednesday|thursday|friday)/i 

so match sentence like: "i'd oil change on wednesday."

this gives me nice $matches array looks this:

array   0 => string 'i'd oil change on wednesday' (length=42)   1 => string 'oil change' (length=10)   2 => string 'wednesday' (length=9) 

the difficulty arises possible or necessary regex contains other (...) patterns. in example wouldn't need it, shows point:

(.*)(get).*~service-type(.*)~day expands /(.*)(get).*(oil change|rotation|brake|tune up)(.*)(monday|tuesday|wednesday|thursday|friday)/i 

which results in $matches being:

array   0 => string 'i'd oil change on wednesday' (length=42)   1 => string 'i'd ' (length=12)   2 => string 'get' (length=3)   3 => string 'oil change' (length=10)   4 => string ' on ' (length=4)   5 => string 'wednesday' (length=9) 

what i'm looking quick , elegant way allow me in either case generate array like:

array   'service-type' => string 'oil change' (length=10)   'day' => string 'wednesday' (length=9) 

with elegant mean don't have parse pattern myself find out how many , @ locations there (...) patterns , inserted expanded concept words. if there's no better way please tell me too, can stop agonizing whether there nice way , bite bullet.

thanks

this seems achieve using named patterns in regex. see http://uk3.php.net/preg_match#example-4885


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -