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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.