blackbad88 Posted November 21, 2009 Share Posted November 21, 2009 I am really trying to understand the syntax of regex patterns but it's just not clicking. But I want to pull the content and attribute from so div tags: <div class="Static" title="Dynamic">This is content that will be read!</div> This will not change: '<div class="Static" title="' but the stuff in red will vary. This is what I have so far, it pulls the content in the div tags but not the attribute tag: preg_match_all("#<div class=\"Static[^>]+>([^<]+)</div>#s", $theData, $matches); foreach ( $matches[1] as $key => $parentName ) { echo "{$parentName}<br />\n"; } Can someone help me out, hope I explained everything. Thanks inadvanced :-) Quote Link to comment Share on other sites More sharing options...
cags Posted November 21, 2009 Share Posted November 21, 2009 Depends how accurate you were being with your description, but something like this should do the trick... '#<div class="Static" title="([^"]*)"[^>]*>(.*?)</div>#s' Quote Link to comment Share on other sites More sharing options...
.josh Posted November 21, 2009 Share Posted November 21, 2009 use dom Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted November 21, 2009 Share Posted November 21, 2009 Using domxpath as an example: $html = <<<EOF <div class="nonStatic" title="Who cares">This is not important!</div> <div class="Static" title="Dynamic">This is content that will be read!</div> <div title="I still don't care">This is STILL not important!</div> EOF; $dom = new DOMDocument; libxml_use_internal_errors(true); $dom->loadHTML($html); // change loadHTML to loadHTMLFile and put full live site url in quotes within parenthesis libxml_use_internal_errors(false); $xpath = new DOMXPath($dom); $divTag = $xpath->query('//div[@class = "Static" and @title]'); foreach ($divTag as $val) { echo $val->getAttribute('title') . ' - ' . $val->nodeValue . "<br />\n"; } Quote Link to comment Share on other sites More sharing options...
blackbad88 Posted November 22, 2009 Author Share Posted November 22, 2009 You guys are awesome! I think Im going to take the domxpath route. But one thing! Is their anyway to make the query non case specific? Thanks again! You are the best! Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted November 22, 2009 Share Posted November 22, 2009 One way to deal with case insensitivities could be to use the xpath translate() functionality, like so: $divTag = $xpath->query('// div[translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="static" and @title]'); Rather large and cumbersome looking, I know... But I do know there is a lower-case() functionality, but I couldn't get that working. Perhaps I overlooked something... not entirely sure. In any case, that should work (I added a space between // and div, otherwise the second forward slash and div vanishes within the snippet). 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.