php - Replace the wrapper (parent) string by regular expression and preserve the inner child content -
i have multiple files have obsolete format , replace them latest format. 1 of trouble finding wrapper string , replace them while have preserve inner content.
example
i want convert expression abc($ignore$)
xbd([$ignore$])->t
. here string format of needs updating.
input:
... abc( ...... ..... (inner content should not changed) ..... ......) ....
output:
... xbd([ ...... ..... (inner content should not changed) ..... ......])->t ....
how achieve it?
you can use following regex able match nested round brackets:
\babc(\(((?>[^()]+)|(?-2))*\))
replace xbd$1->t
.
see demo
<?php $re = "#\babc(\(((?>[^()]+)|(?-2))*\))#"; $str = "abc( ......\n..... (inner content should not changed) .....\n......) "; $subst = "xbd$1->t"; echo $result = preg_replace($re, $subst, $str); ?>
Comments
Post a Comment