Jump to content

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

If also the class is different, make something like [\w\d]+ instead of votes.

Anyway, the /s in the last position allows you to look over the lineedges ;)

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.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.