Codesnippets und Typst
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
# Codesnippets
|
||||
|
||||
## PHP
|
||||
|
||||
### IPV4 --> CIDR
|
||||
|
||||
```php
|
||||
function subnetMaskToCidr(string $mask): int
|
||||
{
|
||||
$long = ip2long($mask);
|
||||
|
||||
if ($long === false) {
|
||||
throw new InvalidArgumentException("Ungültige Subnetzmaske");
|
||||
}
|
||||
|
||||
$cidr = 0;
|
||||
|
||||
for ($i = 31; $i >= 0; $i--) {
|
||||
if (($long & (1 << $i)) !== 0) {
|
||||
$cidr++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $cidr;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
oder
|
||||
|
||||
```php
|
||||
function subnetMaskToCidr(string $mask): int
|
||||
{
|
||||
$octets = explode('.', $mask);
|
||||
$cidr = 0;
|
||||
|
||||
foreach ($octets as $octet) {
|
||||
while ($octet > 0) {
|
||||
$cidr++;
|
||||
$octet &= $octet - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $cidr;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user