tags:

views:

38

answers:

4

Hi!

I have a bunch of text and HTML and what I want to achieve is gather all css classes of img tags that match a certain pattern and place them in a loop.

For example:

<img src="sample1.gif' class="get-this-tag-1" />This is text. This is text. This is text. This is text. This is text. This is text. <img src="sample2.gif' class="image" />This is text. This is text. This is text. This is text. This is text. This is text. <img src="sample3.gif' class="get-this-tag-2" />This is text. This is text. This is text. This is text. This is text. This is text.

In the sample we have 3 images with different classes: get-this-tag-1, image and get-this-tag-2. I only want to retrieve the classes that match get-this-tag- and have them in a loop.

foreach ($classes as $class) {

  //do something

}

Is this possible? Or is there a more optimal way of doing what I want to achieve?

Thank you in advance!

A: 

First of all, you shouldn't process HTML with RegExes. You may well process the class names with RegExes, but not the HTML. That should be done using a proper parser.

Your example would be trivial to do using Javascript and is a one-liner in jQuery. As such, you may want to look into phpquery, which makes it easy for PHP too. In my experience it's a rather slow affair for large sites though.

deceze
Thanks for pointing me in the right direction. I used loadHTML and xpath to parse the HTML and it works great. :)
druesome
A: 

I've done this is regex, but it's a lot less painful with the newer DOM functions. You didn't specify what language you are using, but it appears it's php, which has adequate functions for this job:

http://us.php.net/dom

If you still want to use regex, this might get you started:

$matches = array();
preg_match_all ( '|<img.*>|siU', $data, $matches, PREG_PATTERN_ORDER );
print_r($matches);
Hans
A: 

Do you need to know the class names in the server, or on the browser end? If it is just the browser end, I would recommend using jquery to get the data from javascript.

iWantSimpleLife
Hi, I figured it out the hard way. Used this to get the image classes with jquery:$j("img").each(function() {var cname = $j(this).attr("class")do stuff...});I think this is the best way to approach it. Thanks for your suggestion :)
druesome
I think the more efficient way would be to use $j("img.class") or $j("img[class=someclass]") to get all the image references with the class.
iWantSimpleLife
+1  A: 

I'm not sure to well understand what you mean, but if you want to retrieve only the classes name, you can do :

preg_match_all("/get-this-tag-\d+/", $string, $classes);

All the classes name begin whith get-this-tag will be in array $classes.

M42