Page 1 of 1

generic preg_replace

Posted: Tue Jan 19, 2010 2:23 am
by bartfor
string1:

Code: Select all

employee_id, 13
string2:

Code: Select all

employee_id, 13, status_id, 35
string3:

Code: Select all

employee_id, 13, status_id, 35, menu_id, 4
string4:

Code: Select all

etc..
So its always \w+,[0-9]+

I would like to convert the strings into:

1: employee_id=13
2: employee_id=13 AND status_id=35
3: employee_id=13 AND status_id=35 AND menu_id = 4

with one and the same expression

for string 1 it would be:

Code: Select all

preg_replace('(\w+),([0-9]+)','$1=$2 ' $string);
But I have a hard time making it generic for an x number of parameter additions

Any suggestions?

Re: generic preg_replace

Posted: Tue Jan 19, 2010 3:39 am
by bartfor
Found the answer myself.

Code: Select all

preg_replace(array('/([0-9]+),(\w+)/', '/(\w+),([0-9]+)/'),array('$1 AND $2', '$1=$2'),$string);