简体   繁体   English

在表中回显部分隐藏的(IPV4或IPV6)ip

[英]echo partially hidden (IPV4 or IPV6) ip in a table

I have a table where a variable in a row containing IP information is being echoed. 我有一个表,其中包含IP信息的行中的变量正在被回显。 I think there's an issue with my if statement, because if I use the following then I can get the variable to echo: 我认为if语句存在问题,因为如果使用以下命令,则可以获取变量以进行回显:

echo $row['log_ip'] =  substr_replace ($row['log_ip'], $ipv4replacement, stripos     ($row['log_ip'], $ipv4needle, $offset = 2)); 

My current code: 我当前的代码:

$ipv6needle = ':';
$ipv4needle = '.';
$ipv4replacement = '.***.***.***';
$ipv6replacement = ':****:****:****:****:****:****:****';
if (strpos($row['log_ip'], ':') !== FALSE) {
echo $row['log_ip'] =  substr_replace ($row['log_ip'], $ipv4replacement, stripos     ($row['log_ip'], $ipv4needle, $offset = 2));
else 
echo $row['log_ip'] =  substr_replace ($row['log_ip'], $ipv6replacement, stripos     ($row['log_ip'], $ipv6needle, $offset = 2)); }

Your code is full of weird assignments, I don't know what you want to achieve with them, but here's a corrected and refactored code. 您的代码充满了怪异的任务,我不知道您想用它们实现什么,但这是一个经过纠正和重构的代码。

<?php

$offset = 2;

if (strpos($row["log_ip"], ":") !== false) {
  $needle      = ".";
  $replacement = ".***.***.***";
}
else {
  $needle      = ":";
  $replacement = ":****:****:****:****:****:****:****";
}

$row["log_ip"] = substr_replace($row["log_ip"], $replacement, stripos($row["log_ip"], $needle, $offset));

echo $row["log_ip"];

Answer to the question in the comment: 回答评论中的问题:

<?php

function mask_ip_address($ip_address) {
  if (strpos($ip_address, ".") !== false) {
    $parts = explode(".", $ip_address);
    return $parts[0] . str_repeat(".***", 3);
  }
  $parts = explode(":", $ip_address);
  return $parts[0] . str_repeat(":****", 7);
}

function mask_ip_address_test($ip_address, $expected) {
  assert($expected === mask_ip_address($ip_address));
}

mask_ip_address_test("17.0.0.1", "17.***.***.***");
mask_ip_address_test("fe80::200:5aee:feaa:20a2", "fe80:****:****:****:****:****:****:****");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM