Reputation: 1372
In this pattern:
$pattern = '/(\s*)@(if|foreach)(\s*\(.*\))/';
How can I replace every # with $ on (\s*(.*))
This will be used afterwards:
preg_replace($pattern, '$1<?php $2$3: ?>', $value);
Upvotes: 0
Views: 78
Reputation: 92996
You are looking for preg_replace_callback.
There you can define an anonymous function that returns the replacement, where you would be able to perform your additional task on group 3 and then build the replacement as you need.
From the example in the second link
<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
You can see that you can access the group 3 like this: $match[3]
Upvotes: 1