Answer the question
In order to leave comments, you need to log in
How to write a complex regexp condition?
How to turn it
<chunk></chunk>
<chunk></chunk>
<chunk></chunk>
<span data-element="chunk" name="chunk.name.test"></span>
<span data-element="chunk" name="tpl.test.name"></span>
<span data-element="chunk" name="test"></span>
Answer the question
In order to leave comments, you need to log in
<?php
$str = '<chunk></chunk><chunk></chunk><chunk></chunk>';
$replace = '<span data-element="$1" name="$3">$2</span>';
echo preg_replace("/<(\w+)>(\[\+)\]\])<\/(\w+)>/u", $replace, $str);
$ php replace.php
<span data-element="chunk" name="chunk.name.test"></span>
<span data-element="chunk" name="tpl.test.name"></span>
<span data-element="chunk" name="test"></span>
<?php
$str = '<chunk></chunk><chunk></chunk><chunk></chunk>';
$replace = '<span data-element="$1" name="$3">$2</span>';
$allow = 'chunk.name.test|test';
echo preg_replace("/<(\w+)>(\[\[[$!]+((?:{$allow}))\]\])<\/(\w+)>/u", $replace, $str);
$ php replace.php
<span data-element="chunk" name="chunk.name.test"></span>
<chunk></chunk>
<span data-element="chunk" name="test"></span>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question