PHP Replace second occurence of ? with an & -


i have few strings, if string below

$string ="w?w+f=w?wefwef?blahblah";  $desiredstring ="w?w+f=w&wefwef?blahblah"; 

if format, trying replace second ? & else nothing.

i've tried regex , preg_match no luck.

in current case solution is:

$string ="w?w+f=w?wefwef?blahblah";  $desiredstring = preg_replace('/^(.+?\?.+?)\?/', '$1&', $string);  echo $desiredstring; 

output:

w?w+f=w&wefwef?blahblah 

n-th occurence regexp:

/^((?:(?:.*?\?){0}.*?))\?/ // first occurence /^((?:(?:.*?\?){1}.*?))\?/ // second occurence /^((?:(?:.*?\?){2}.*?))\?/ // third occurence                            // etc.