Jump to content

preg_match that pulls both the attribute and content of div tag.


blackbad88

Recommended Posts

I am really trying to understand the syntax of regex patterns but it's just not clicking. :shrug: 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 :-)

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";
}

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

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.