My regex (<(div|i)\b[^>]*>)(\bTEST\b)(<\/(div|i)\s*>)
replace = 'MOM' From
<div clas="dfsdf">TEST</div>
is
MOM
But I want the HTML tag persists
It should look like this: <div clas="dfsdf">MOM</div>
What am I doing wrong?
My regex (<(div|i)\b[^>]*>)(\bTEST\b)(<\/(div|i)\s*>)
replace = 'MOM' From
<div clas="dfsdf">TEST</div>
is
MOM
But I want the HTML tag persists
It should look like this: <div clas="dfsdf">MOM</div>
What am I doing wrong?
If you just replace with MOM, your whole match will be replaced by MOM..In your regex <div clas="dfsdf"> is group 1 ($1), TEST is capture group 3 ($3) and </div> is group 4 ($4)... So, you have to replace it with the following
$1MOM$4
to contain what is required and replace just the capture group corresponding to TEST.
You could use look around:
preg_replace('~(?<=<(?:div|i)\b[^>]*>)TEST(?=</\s*(?:div|i)\s*>)~', "MOM', $str);