1
<ul class="vehicle__gallery cf">
<li><a href="#"><img src="AETV19098412_2a.jpg"></a></li>
<li><a href="#"><img src="AETV19098412_3a.jpg"></a></li>
<li><a href="#"><img src="AETV19098412_4a.jpg"></a></li>
</ul>

and my preg match syntax is as below:

preg_match_all('/<ul class="vehicle__gallery cf">.*?<li>.*?<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>.*?<\/li>.*?<\/ul>/s', $html_image,$posts, PREG_SET_ORDER);
Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
sudhakar
  • 11
  • 2
  • Are you able to use JavaScript or better JQuery? Then it is way mor easy and clean to extract the data you need and pass it via Ajax becaues this can work but if you make only add one parameter later or maybe do an accidental space between html attributes this code will not work anymore. Its a very hacky solution. Otherwise, are you not able to use a form instead to pass your image data to your script? – Steini Aug 22 '14 at 04:55

3 Answers3

0

You dont use regex to parse HTML.It wont work.

  • <li> tags dont always have ending tag nor do <img> tag.
  • There can be n number of attributes to a tag
  • attribute values don't always go in double quotes

Use an html parser like simpledomparser

I wont even attempt to come up with a regex for this because at some point it would fail.

Community
  • 1
  • 1
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • @Anirudha your answer `Use an html parser like simpledomparser` would be a comment not as an answer. Provide the solution for how to get the values inside ` – Avinash Raj Aug 22 '14 at 05:41
  • @AvinashRaj OP is asking for a regex solution and the ans to it is "no". As far as the solution is concerned let him post a new question if he's facing any problem using the library. That would be another question and to be honest i dont know PHP – Anirudha Aug 22 '14 at 05:45
-1

If you give your img tags a class or something, for example:

<img class="gallery_item" src="AETV19098412_2a.jpg">
<img class="gallery_item" src="AETV19098412_3a.jpg">

you can do more easy:

preg_match('/<img class="gallery_item" src="(.*)">/');

However this is still very hacky, if you ever add a css class, html attributes or modify your code you have the problem that your code might not work anymore.

This solution is anything else then clean and you should considerung using JQuery or a form as stated in my comment before would make your life alot easier and the code will not break because of future, minor html changes that might come up any day.

Steini
  • 2,753
  • 15
  • 24
  • hi, `.*` should be `.*?`..although there are a lot of cases where the regex would fail,am afraid.. – Anirudha Aug 22 '14 at 05:10
  • Well I stated that this is very risky, I just provided a PHP-based solution because the author asked for it. And (.*) is correct and will work aslong as there are no extra spaces or any other attributes interfering here. – Steini Aug 22 '14 at 05:14
-1

Another approach is use javascript (jquery).

var imgArr = []
$("ul.vehicle__gallery li img").each(function(){
     imgArr.push($(this).attr('src'));
})
fanfan1609
  • 179
  • 2
  • 8
  • This would work fine but we need to check back with OP if he's fine using jquery. He's using php (P.S i didnt downvote) – Anirudha Aug 22 '14 at 05:14
  • Yes, I just give my solutions based on my usual thinking.. Anyway, if he is votedown, I will remove my answer. – fanfan1609 Aug 22 '14 at 05:30