PHP
- 12 ответов
- 0 вопросов
8
Вклад в тег
$doc = new DOMDocument();
$doc->loadHTML($htmlAsString);
$links = $doc->getElementsByTagName('a');
function filterByHref(DOMNodeList $anchors, string $hrefRegex): array {
$links = [];
/** @var DOMElement $elm */
foreach ($anchors as $elm) {
if (! $elm->hasAttribute('href')) {
continue;
}
$link = $elm->getAttribute('href');
if (preg_match($hrefRegex, $link)) {
$links[] = $link;
}
}
return $links;
}
var_export([
'cnt' => $links->length,
'goo' => filterByHref($links, '|^https://google.com\b|'),
'link' => filterByHref($links, '|link1|i'),
], false);
$arr = \json_decode($a, true);
$reducer = function ($carry, $item): float {
if ('qw_wallet_rub' === $item['alias'] ?? null) {
return $carry + $item['balance']['amount'] ?? 0.0;
}
return (float)$carry;
};
$result = \array_reduce($arr['accounts'], $reducer);
$firstIpAsString = '77.88.0.0';
$lastIpAsString = '77.88.0.18';
$ipAsString = '77.88.0.111';
$firstIp = ip2long($firstIpAsString);
$lastIp = ip2long($lastIpAsString);
$ip = ip2long($ipAsString);
var_export([
'first' => $firstIp,
'last' => $lastIp,
'ip' => $ip,
'in range?' => $firstIp <= $ip && $ip <= $lastIp,
], false);