本文整理汇总了PHP中stdClass::item方法的典型用法代码示例。如果您正苦于以下问题:PHP stdClass::item方法的具体用法?PHP stdClass::item怎么用?PHP stdClass::item使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stdClass
的用法示例。
在下文中一共展示了stdClass::item方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: grabArticle
protected function grabArticle($page = null)
{
if (!$page) {
$page = $this->dom;
}
$xpath = null;
$nodesToScore = array();
if ($page instanceof DOMDocument && isset($page->documentElement)) {
$xpath = new DOMXPath($page);
}
$allElements = $page->getElementsByTagName('*');
for ($nodeIndex = 0; $node = $allElements->item($nodeIndex); $nodeIndex++) {
$tagName = $node->tagName;
// Some well known site uses sections as paragraphs.
if (strcasecmp($tagName, 'p') === 0 || strcasecmp($tagName, 'td') === 0 || strcasecmp($tagName, 'section') === 0) {
$nodesToScore[] = $node;
}
// Turn divs into P tags where they have been used inappropriately
// (as in, where they contain no other block level elements).
if (strcasecmp($tagName, 'div') === 0 || strcasecmp($tagName, 'article') === 0 || strcasecmp($tagName, 'section') === 0) {
if (!preg_match($this->regexps['divToPElements'], $node->innerHTML)) {
//$this->dbg('Altering '.$node->getNodePath().' to p');
$newNode = $this->dom->createElement('p');
try {
$newNode->innerHTML = $node->innerHTML;
// It's easier to debug using original attributes.
//$newNode->setAttribute('class', $node->getAttribute('class'));
//$newNode->setAttribute('id', $node->getAttribute('id'));
$node = $node->parentNode->replaceChild($newNode, $node);
$nodeIndex--;
$nodesToScore[] = $newNode;
} catch (Exception $e) {
$this->dbg('Could not alter div/article to p, reverting back to div: ' . $e->getMessage());
}
} else {
// Will change these P elements back to text nodes after processing.
for ($i = 0, $il = $node->childNodes->length; $i < $il; $i++) {
$childNode = $node->childNodes->item($i);
if (is_object($childNode) && get_class($childNode) === 'DOMProcessingInstruction') {
//executable tags (<?php or <?xml) warning
$childNode->parentNode->removeChild($childNode);
continue;
}
if ($childNode->nodeType == 3) {
// XML_TEXT_NODE
//$this->dbg('replacing text node with a P tag with the same content.');
$p = $this->dom->createElement('p');
$p->innerHTML = $childNode->nodeValue;
$p->setAttribute('data-readability-styled', 'true');
$childNode->parentNode->replaceChild($p, $childNode);
}
}
}
}
}
/**
* Loop through all paragraphs, and assign a score to them based on how content-y they look.
* Then add their score to their parent node.
*
* A score is determined by things like number of commas, class names, etc.
* Maybe eventually link density.
**/
for ($pt = 0, $scored = count($nodesToScore); $pt < $scored; $pt++) {
$parentNode = $nodesToScore[$pt]->parentNode;
// No parent node? Move on...
if (!$parentNode) {
continue;
}
$grandParentNode = $parentNode->parentNode instanceof DOMElement ? $parentNode->parentNode : null;
$innerText = $this->getInnerText($nodesToScore[$pt]);
// If this paragraph is less than MIN_PARAGRAPH_LENGTH (default:20) characters, don't even count it.
if (mb_strlen($innerText) < self::MIN_PARAGRAPH_LENGTH) {
continue;
}
// Initialize readability data for the parent.
if (!$parentNode->hasAttribute('readability')) {
$this->initializeNode($parentNode);
$parentNode->setAttribute('data-candidate', 'true');
}
// Initialize readability data for the grandparent.
if ($grandParentNode && !$grandParentNode->hasAttribute('readability') && isset($grandParentNode->tagName)) {
$this->initializeNode($grandParentNode);
$grandParentNode->setAttribute('data-candidate', 'true');
}
// Add a point for the paragraph itself as a base.
$contentScore = 1;
// Add points for any commas within this paragraph.
$contentScore += $this->getCommaCount($innerText);
// For every SCORE_CHARS_IN_PARAGRAPH (default:100) characters in this paragraph, add another point. Up to 3 points.
$contentScore += min(floor(mb_strlen($innerText) / self::SCORE_CHARS_IN_PARAGRAPH), 3);
// For every SCORE_WORDS_IN_PARAGRAPH (default:20) words in this paragraph, add another point. Up to 3 points.
$contentScore += min(floor($this->getWordCount($innerText) / self::SCORE_WORDS_IN_PARAGRAPH), 3);
/* TEST: For every positive/negative parent tag, add/substract half point. Up to 3 points. *\/
$up = $nodesToScore[$pt];
$score = 0;
while ($up->parentNode instanceof DOMElement) {
$up = $up->parentNode;
if (preg_match($this->regexps['positive'], $up->getAttribute('class') . ' ' . $up->getAttribute('id'))) {
$score += 0.5;
} else if (preg_match($this->regexps['negative'], $up->getAttribute('class') . ' ' . $up->getAttribute('id'))) {
//.........这里部分代码省略.........
示例2: grabArticle
protected function grabArticle($page = null)
{
$stripUnlikelyCandidates = $this->flagIsActive(self::FLAG_STRIP_UNLIKELYS);
if (!$page) {
$page = $this->dom;
}
$allElements = $page->getElementsByTagName('*');
/**
* First, node prepping. Trash nodes that look cruddy (like ones with the class name "comment", etc), and turn divs
* into P tags where they have been used inappropriately (as in, where they contain no other block level elements.)
*
* Note: Assignment from index for performance. See http://www.peachpit.com/articles/article.aspx?p=31567&seqNum=5
* TODO: Shouldn't this be a reverse traversal?
**/
$node = null;
$nodesToScore = array();
for ($nodeIndex = 0; $node = $allElements->item($nodeIndex); $nodeIndex++) {
//for ($nodeIndex=$targetList->length-1; $nodeIndex >= 0; $nodeIndex--) {
//$node = $targetList->item($nodeIndex);
$tagName = strtoupper($node->tagName);
/* Remove unlikely candidates */
if ($stripUnlikelyCandidates) {
$unlikelyMatchString = $node->getAttribute('class') . $node->getAttribute('id');
if (preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString) && !preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString) && $tagName != 'BODY') {
$this->dbg('Removing unlikely candidate - ' . $unlikelyMatchString);
//$nodesToRemove[] = $node;
$node->parentNode->removeChild($node);
$nodeIndex--;
continue;
}
}
if ($tagName == 'P' || $tagName == 'TD' || $tagName == 'PRE') {
$nodesToScore[] = $node;
}
/* Turn all divs that don't have children block level elements into p's */
if ($tagName == 'DIV') {
if (!preg_match($this->regexps['divToPElements'], $node->innerHTML)) {
//$this->dbg('Altering div to p');
$newNode = $this->dom->createElement('p');
try {
$newNode->innerHTML = $node->innerHTML;
//$nodesToReplace[] = array('new'=>$newNode, 'old'=>$node);
$node->parentNode->replaceChild($newNode, $node);
$nodeIndex--;
$nodesToScore[] = $node;
// or $newNode?
} catch (Exception $e) {
$this->dbg('Could not alter div to p, reverting back to div.: ' . $e);
}
} else {
/* EXPERIMENTAL */
// TODO: change these p elements back to text nodes after processing
for ($i = 0, $il = $node->childNodes->length; $i < $il; $i++) {
$childNode = $node->childNodes->item($i);
if ($childNode->nodeType == 3) {
// XML_TEXT_NODE
//$this->dbg('replacing text node with a p tag with the same content.');
$p = $this->dom->createElement('p');
$p->innerHTML = $childNode->nodeValue;
$p->setAttribute('style', 'display: inline;');
$p->setAttribute('class', 'readability-styled');
$childNode->parentNode->replaceChild($p, $childNode);
}
}
}
}
}
/**
* Loop through all paragraphs, and assign a score to them based on how content-y they look.
* Then add their score to their parent node.
*
* A score is determined by things like number of commas, class names, etc. Maybe eventually link density.
**/
$candidates = array();
for ($pt = 0; $pt < count($nodesToScore); $pt++) {
$parentNode = $nodesToScore[$pt]->parentNode;
// $grandParentNode = $parentNode ? $parentNode->parentNode : null;
$grandParentNode = !$parentNode ? null : ($parentNode->parentNode instanceof DOMElement ? $parentNode->parentNode : null);
$innerText = $this->getInnerText($nodesToScore[$pt]);
if (!$parentNode || !isset($parentNode->tagName)) {
continue;
}
/* If this paragraph is less than 25 characters, don't even count it. */
if (strlen($innerText) < 25) {
continue;
}
/* Initialize readability data for the parent. */
if (!$parentNode->hasAttribute('readability')) {
$this->initializeNode($parentNode);
$candidates[] = $parentNode;
}
/* Initialize readability data for the grandparent. */
if ($grandParentNode && !$grandParentNode->hasAttribute('readability') && isset($grandParentNode->tagName)) {
$this->initializeNode($grandParentNode);
$candidates[] = $grandParentNode;
}
$contentScore = 0;
/* Add a point for the paragraph itself as a base. */
$contentScore++;
/* Add points for any commas within this paragraph */
//.........这里部分代码省略.........