本文整理汇总了PHP中Symfony\Component\DomCrawler\Crawler::each方法的典型用法代码示例。如果您正苦于以下问题:PHP Crawler::each方法的具体用法?PHP Crawler::each怎么用?PHP Crawler::each使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\DomCrawler\Crawler
的用法示例。
在下文中一共展示了Crawler::each方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: format
public function format(Crawler $crawler, array $config)
{
$locations = $config['locations'];
$return = $crawler->each(function (Crawler $node, $i) use($locations) {
// Symfony dom-crawler methods ignore text nodes, so we have to
// revert to native DOMNode methods instead.
$children = $node->getNode(0)->childNodes;
$values = [];
foreach ($children as $child) {
// Skip non element or text nodes.
if (!in_array($child->nodeType, [1, 3])) {
continue;
}
// Loop through locations but stop after the first truthy value.
foreach ($locations as $location) {
if ('_text' === $location && ($value = trim($child->nodeValue))) {
$values[] = $value;
continue 2;
}
// DOMText does not have method hasAttribute.
if (1 === $child->nodeType && $child->hasAttribute($location) && ($value = trim($child->getAttribute($location)))) {
$values[] = $value;
continue 2;
}
}
}
// Remove any lingering 'empty' elements.
$value = implode(' ', array_filter(array_map('trim', $values)));
if ($this->looksLikeGroupTitle($node)) {
$value = '%%TITLE%%' . $value . '%%TITLE%%';
}
return $value;
});
return [array_shift($return)];
}
示例2: __construct
/**
* @param Crawler $nodes
*/
public function __construct(Crawler $nodes)
{
$this->nodes = [];
$nodes->each(function ($node) {
$this->nodes[] = $node;
});
}
示例3: getXml
protected function getXml(Crawler $crawler)
{
$document = new \DOMDocument('1.0', 'utf-8');
$crawler->each(function (\DOMElement $node) use($document) {
$document->appendChild($document->importNode($node, true));
});
return html_entity_decode($document->saveXML());
}
示例4: format
public function format(Crawler $crawler, array $config)
{
$locations = $config['locations'];
$return = $crawler->each(function (Crawler $node, $i) use($locations) {
$values = $node->extract($locations);
/**
* Remove falsey values and normalize result...
*
* If 1 == count($locations), $values will contain strings,
* otherwise $values will contain arrays.
*/
if (is_array($values[0])) {
$values = array_filter($values[0]);
}
if ($this->looksLikeGroupTitle($node)) {
$values = array_map(function ($value) {
return '%%TITLE%%' . $value . '%%TITLE%%';
}, $values);
}
return array_shift($values);
});
return array_values(array_filter($return));
}
示例5: setCardFromMD
/**
* create card from microdata && save it in cardContainer
*
* @param Crawler $scope
* @param string $url
*/
private function setCardFromMD(Crawler $scope, $url)
{
$scope->each(function (Crawler $node) use($url) {
try {
$type = $this->parser->getCardType($node);
$card = $this->createCard($type);
} catch (\RuntimeException $e) {
$card = $this->createCard('Thing');
}
if ($node->attr('itemprop')) {
$this->childArray[] = $card;
}
$card->child = count($node->filter('[itemscope]')) - 1;
$card->url = $url;
$this->parser->setCardProperties($node, $card);
$this->saveCard($card);
});
}
示例6: getCasts
private function getCasts(Crawler $links)
{
$staffs = [];
$links->each(function (Crawler $link, $i) use(&$staffs) {
if (!preg_match('/id=(\\d+)/', $link->attr('href'), $matches)) {
return;
}
$staff = new Staffs();
list($name, $aka) = CrawlDmmTask::parseNameAndAka(trim($link->text()));
$staff->name = $name;
$staff->aka = $aka ? implode(',', $aka) : null;
$staff->id = $matches[1];
$staffs[] = $staff;
});
return $staffs;
}