barudo
barudo

Reputation: 665

Remove tags inside attributes

I have this string that was passed to a Text_Diff...

<?php
$left_string = '<div class="class1" style="display:block;">Some Text<del> Orig</del></div>';
$right_string = '<div class="class1" style="<ins>color:#FFF;</ins>;display:block;">Some Text</div>';

There are only two possible tags namely: del and ins. I'm not to remove these tags if ever they are not inside tags. But need to remove them when they are inside attributes.

Upvotes: 3

Views: 96

Answers (1)

user1134181
user1134181

Reputation:

You can use search by using the following regular expression:

(?<=style=)([\w\W]+)(?:<ins>|<del>)([\w\W\s]+)(?:<\/ins>|<\/del>)([\w\W]*)(?=">)

The value of the attribute is the text that matches with backward links:

([\w\W]+) == $1

([\w\W\s]+) == $2

([\w\W]*) == $3

Then this combination will give you the required value for the attribute:

$1$2$3

For this input string:

<div class="class1" style="display:block;">Some Text<del> Orig</del></div>

You will get the result:

<div class="class1" style="display:block;">Some Text<del> Orig</del></div>

For this input string:

<div class="class1" style="<ins>color:#FFF;</ins>;display:block;">Some Text</div>

You will get the result:

<div class="class1" style="color:#FFF;;display:block;">Some Text</div>

For this input string:

<div class="class1" style=";display:block;<ins>color:#FFF;</ins>">Some Text</div>

You will get the result:

<div class="class1" style=";display:block;color:#FFF;">Some Text</div>

See demo here: https://regex101.com/r/3XKv5s/1


For any attribute, not only style:

(?<=[a-zA-Z]=")([\w\W]*)(?:<ins>|<del>)([\w\W\s]*)(?:<\/ins>|<\/del>)([\w\W]*)(?=">)

See demo here: https://regex101.com/r/3XKv5s/2

Upvotes: 1

Related Questions