Search code examples
phpregexpreg-match

How to Regular minimum matching


I have string:

"<p>111111111111</p><p><a href="hhz://user:28902"><img src="http://test.img.hhz.com/Op-imageShow/d84c520hs0bv00000odheqk?iv=1" title="sample.jpeg" alt="sample.jpeg"/></a></p><p><br/></p><p>dfad</p><p><br/></p><p><a href="hhz://photo:0000csh000000jts"><img src="http://test.img.hhz.com/Op-imageShow/3d64330b80bk00000odhewz?iv=1" title="20160914144811.png" alt="20160914144811.png"/></a></p><p><br/></p><p><br/></p><p><img src="http://test.img.hhz.com/App-imageShow/o_nphone/0b5/e4eee20ku0q200000ocuvk2?iv=1" data-photo-id="00003w10000009ae" style="width:300px;height:auto;"/></p><p><br/></p><p><img src="http://test.img.hhz.com/Op-imageShow/7b75220ku08c00000odhff6?iv=1" title="bee7d20rs0b400000oddx35.jpg" alt="bee7d20rs0b400000oddx35.jpg"/></p><p><img src="http://test.img.hhz.com/App-imageShow/o_nphone/e60/8c0c720ku0ku00000oct56s?iv=1" data-photo-id="00003w00000009ae" style="width:300px;height:auto;"/><img src="http://test.img.hhz.com/App-imageShow/o_nphone/f81/19aa920dw0dw00000oa8iht?iv=1" data-photo-id="00001i40000009bk" style="width:300px;height:auto;"/></p>"

I want to only match :

<img src="http://test.img.hhz.com/App-imageShow/o_nphone/e60/8c0c720ku0ku00000oct56s?iv=1" data-photo-id="00003w00000009ae" style="width:300px;height:auto;"/>
<img src="http://test.img.hhz.com/App-imageShow/o_nphone/f81/19aa920dw0dw00000oa8iht?iv=1" data-photo-id="00001i40000009bk" style="width:300px;height:auto;"/>
<img src="http://test.img.hhz.com/App-imageShow/o_nphone/0b5/e4eee20ku0q200000ocuvk2?iv=1" data-photo-id="00003w10000009ae" style="width:300px;height:auto;"/>

currently I use matching:

$pattern = '/\<img\s+src=\"(\w.+)\"\s+data-photo-id=\"(\w{16})\".+\>/U';

actually, I have a long string that's html text. Some image tags have data-photo-id attributes, and some did not, but I want to matching contains img data-photo-id Tags. The problem is that I can't match the regular. please someone help me.


Solution

  • Try that :

    '/\<img\s+(src=\"\w[^\>]+\")[^\>]*(data-photo-id=\"\w{16}\")[^\>]*\/\>/U'
    

    verification : https://regex101.com/r/eS9oU1/2

    EDIT: It follows yours next modifications