php - Regex, exclude filename in html

Html Code
<div class="thx_thanked_post"><a title="Click to enlarge" target="_blank" data-fancybox="data-2581" data-type="image" href="images/thx/star.png"><img src="images/thx/star.png" alt="Better response on post dqwdqwdqwqdwdqwqwdwdwd" class="thx_thanked_post_img"></a>Just a "quick" overview of what's good and why.<br>
<a title="Click to enlarge" target="_blank" data-fancybox="data-2581" data-type="image" href="images/thx/kek.png"><img src="images/thx/kek.png" alt="Better response on post dqwdqwdqwqdwdqwqwdwdwd" class="thx_thanked_post_img"></a>
<br>
Note: Hyper speed is referenced multiple times here. This is a bug(?) where after reaching speeds over 160 km/h, you continue accelerating like crazy while traveling in a straight line, allowing you to reach speeds of over 500 km/h with a good boost under ideal conditions. Making good use of hyper speed when possible is huge for cutting time, and much of what makes something good is how easily and consistently it works to give hyper speed.<br>
I want to exclude<img>
tag that has "stars.png", enclosed by<a>
tag.
My regex:
<a [^>]*\>([\t]|[\n])*<img[^>]*^(?!.*star\.png$)[^>]*\>([\t]|[\n])*<\/a>
It doesn't work. This picks up nothing.
Correct match should only pickup 2nd<a>
tag with<img>
that has "images/thx/kek.png" but not "images/thx/star.png".
Answer
Solution:
<a [^>]*>([\t]|[\n])*<img(?!.*star\.png)[^>]*>([\t]|[\n])*<\/a>
seems to work.
^
and$
match the start and the end of the line, and they are likely not what you want to match, so I remove them.[^>]*
matches all the content inimg
tag and stops just before the closing>
. Lookahead assertion(?!.*star\.png)
after it starts matching from that>
and not the content inimg
tag, so I move it in front of[^>]*
.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: malformed utf-8 characters, possibly incorrectly encoded
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.