Jump to content

Regex to find information inside of a div


dhcrusoe

Recommended Posts

Hey guys,

 

Trying this for hours... any pointers? I'm trying to build a Regex that will find information inside of a div:

 

<div class=votes>some_number</div>

 

and here's what I've got:

 

if (preg_match_all('/<div class=votes>.*?<\/div>/',$s,$m,PREG_SET_ORDER))

 

what's going wrong here? I keep getting no info from the regex... :( 

 

Thanks for your help!

 

--Dave / CK

http://www.codekindness.org

 

 

Try the following expression:

/\<div class\=[\"]{0,1}votes[\"]{0,1}\>(.*?)\<\/div\>/s

 

That sample has issues however. You don't need to escape characters like <, >, = or " (for the last one: assuming the entire pattern is encased in single quotes as in the initial sample provided by the OP).

You can also express [\"]{0,1} as [\'"]? (single or double quotes, optional)

 

If I was to re-write this, I would even consider extra precautions as well as implementing conditionals:

 

$str = '<div> insert no-classed div content here... </div><div class="votes"> insert div content here... </div><div> insert no-classed div content here... </div>';
preg_match_all('#<div\b[^>]*class=([\'"])?votes(?(1)[\'"])[^>]*>.*?</div>#s', $str, $matches);
echo '<pre>'.print_r($matches[0], true);

 

Output: (when viewing end results in soure code)

Array
(
    [0] => <div class="votes"> insert div content here... </div>
)

 

Because of class=([\'"])?votes(?(1)[\'"]), if there is quotes (single or double) on one side, there must be either single or double quotes on the otherside.. but this solution admittedly permits things like div class="votes' or vise vera: div class='votes"... rather messy...we could alter the code to automatcially require matching single or double quotes by using \1 instead of [\'"]:

preg_match('#<div\b[^>]*class=([\'"])?votes(?(1)\1)[^>]*>.*?</div>#s', $str, $match);

 

(I realize I'm using the s modifier when in this particular example, it isn't necessary.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.