I just making an upgrade script for replacing all the MODX snippet tags to a newer format:
Old one: [~123~]
New one: [[~123]]
(of course, 123 - it is an example — there are a lot of different numbers)
Also I wanted to replace all the custom old snippets to a newer ones:
Old snippet format:
[!MySnippetName? ¶m1=`value1` ¶m2=`value2` !]
New one:
[[!MySnippetName? ¶m1=`value1` ¶m2=`value2` ]]
(of course, ¶m1=value1 ¶m2=value2
is just an example and it is different in real snippets)
How to use a preg_replace function to make a global replacements? I supposed to create like this:
$doc[ 'content' ] = $this->preg_replace_all(
array(
'/\[~([0-9]+)~\]/',
'\[!LatestUpdates(.*)!\]',
'\[!ArticleSummary(.*)!\]',
), array(
'[[~\1]]',
'[[!LatestUpdates\1]]',
'[[!ArticleSummary\1]]',
),
$doc[ 'content' ]
);
preg_replace_all function:
private function preg_replace_all( $find, $replacement, $s )
{
while(preg_match($find, $s)) {
$s = preg_replace($find, $replacement, $s);
}
return $s;
}
But it doesn't work. Please help to figure out the problem. Thanks in advance.