Let’s assume I use the following php function:
preg_replace($search, $replace, $str);
I’m only able to modify the search and replace var. Is it possible to do the following replace:
test -> test test2
And
test 3 -> test3
I tried the following:
$search = '/test (?=*)/';
$replace = 'test($1)';
This works for the second case. But now I want to use a if, in order to say “if $1 is empty, add test2. But I’m only able to modify the search and replace var without using addition php. Is this even possible in regex?
You can pass the search and replace parameters as arrays. So pass an array of search queries into the search parameter, and an array of corresponding replace strings in the replace parameter…
so, you might try: