regex - How to preg_replace_all in the large amount of data -



regex - How to preg_replace_all in the large amount of data -

i making upgrade script replacing modx snippet tags newer format:

old one: [~123~]

new one: [[~123]]

(of course, 123 - illustration — there lot of different numbers)

also wanted replace custom old snippets newer ones:

old snippet format:

[!mysnippetname? &param1=`value1` &param2=`value2` !]

new one:

[[!mysnippetname? &param1=`value1` &param2=`value2` ]]

(of course, &param1=value1 &param2=value2 illustration , different in real snippets)

how utilize preg_replace function create global replacements? supposed create 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); } homecoming $s; }

but doesn't work. please help figure out problem. in advance.

the preg_replace function performs global replacement , since matching substrings replaced, don't need test existence of these substrings preg_match.

you can cut down 3 patterns 1 pattern:

$pattern = '/\[(?|(!latestupdates|!articlesummary)(.*?)!]|(~)([0-9]+)~])/'; $replacement = '[[$1$2]]'; $doc['content'] = preg_replace($pattern, $replacement, $doc['content']);

this pattern utilize branch reset feature allows capture groups have same number in each alternatives.

if possible (if doesn't alter tags want preserve), can same strtr:

$doc['content'] = strtr($doc['content'], array('[!'=>'[[!', '[~'=>'[[~', '~]'=>']]', '!]'=>']]'));

note if can utilize it, don't hesitate since way much faster.

regex preg-replace upgrade modx-revolution modx-evolution

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -