Reputation: 1513
My code in php is:
while(preg_match('%(<span style="color: green;">)(?:\s+)?(</.*?>)%i', $result2)==1){
$result2 = preg_replace('%(<span style="color: green;">)(?:\s+)?(</.*?>)%i', '$2 $1', $result2);
}
Right now I have an input such as:
<span style="color: green;"></p></i>
and what my code does is that whenever any tag is closing after this span green tag, they are placed before span. So the output for the above input would be:
</p></i><span style="color: green;">
I want that if there is any span tag closing after this span green tag, that should be ignored and all other closing tags should be placed first.. Example Input:
<span style="color: green;"></p></span></i>
Output:
</p></i><span style="color: green;"></span>
Can anybody help me out in making this change?
Upvotes: 2
Views: 58
Reputation: 3215
In general for some regex implementations you can use look-ahead mechanism, which would look like (?!</span>)(</.*?>)
instead of just (</.*?>)
in your match/replace pattern.
Otherwise, please check Regex - how to match everything except a particular pattern as it solves the general problem for both standard and non-standard implementations.
You can also rely a bit less on regex, and just check in your loop if the tag you found matches , if so break the loop (if I understand your idea correctly).
Edit: if you want to keep moving the tags after the span is closed, you can just add to accepted 'content', like:
http://sandbox.onlinephpfunctions.com/code/8177a9afbdd5ac7c1660679d9cb362a9b48c29a4
you would probably like to accept something more in the span content, like \w+
(your code is loosing it totally) and if you want many span closing tags you need to make it more generic too, but the idea behind it is the same
Upvotes: 1