dhcrusoe Posted December 30, 2008 Share Posted December 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
dhcrusoe Posted December 30, 2008 Author Share Posted December 30, 2008 something like this? <div class=votes>(.*?)<\/div> Quote Link to comment Share on other sites More sharing options...
.josh Posted December 30, 2008 Share Posted December 30, 2008 That would work if that's exactly what you div tag looks like. Also good chance you probably need to be using the m modifier for multiline mode Quote Link to comment Share on other sites More sharing options...
Lumio Posted January 1, 2009 Share Posted January 1, 2009 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 Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted January 1, 2009 Share Posted January 1, 2009 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.) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.