开发者

Use PHP to locate all elements with a class that starts with "enddate-"?

Thanks in advance for accommodating my glaring PHP limitations.

Consider this HTML:

<div class="story first enddate-2011-04-21">
  <h3>misc HTML content</h3> 
  ...
</div>

<div class="sparkly enddate-2014-01-01">
  <p>blah blah blah</p> 
  ...
</div> 

How can I use PHP to identify all elements in an HTML doc with a class that starts with "enddate-" ?

Why I'd like to know: I want to use PHP开发者_JS百科 (bx it's server-side) to locate all such elements, as in both divs above; then grab (and validate) the implied date info, and, if today's date is after the end date, cause that element and its inner contents to NOT be displayed on the served web page (so, in the above, remove all evidence of the first div now but leave the second one visible until 2014).

I found some advice here on stackoverflow about using a PHP HTML parser to locate elements with a specific ID, as in:

$doc = new DOMDocument();
$doc->loadHTML($html);
$element = $doc->getElementById('ithis');
$element->parentNode->removeChild($element);
$html = $doc->saveHTML();

Can this be modified to look for "class that starts with 'enddate' "?


I think you could do this using XPath.

Something like this should work:

$xpath = new DOMXPath($domdocument);
$elements = $xpath->query("//*[contains(@class, 'enddate-')]");

(The xpath query is untested)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜