Removing tags inside attributes

I have this line that was passed to 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: del and ins. I do not delete these tags if they are not inside the tags. But you need to remove them when they are inside the attributes.

+4
source share
1 answer

You can use the search using the following regular expression:

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

An attribute value is text that matches backlinks:

([\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 line:

<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 line:

<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 line:

<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 the 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]*)(?=">)

: https://regex101.com/r/3XKv5s/2

+1

Source: https://habr.com/ru/post/1656296/


All Articles