Files
Ai-Single-Page/z.ai/ip-replace/ip-replace.php
T

181 lines
5.4 KiB
PHP

<?php
// 设置默认时区(可选,防止警告)
date_default_timezone_set('Asia/Shanghai');
// 处理表单提交
$result_content = '';
$original_content = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$target_ip = trim($_POST['target_ip'] ?? '');
$original_content = trim($_POST['content'] ?? '');
if (!empty($original_content) && !empty($target_ip)) {
// 1. 构建正则表达式 (匹配 #IPv4# 或 #[IPv6]#)
// 为了在替换时区分 IPv4 和 IPv6,我们在正则中使用了两个捕获组:
// Group 1: IPv4 地址
// Group 2: IPv6 地址 (包含括号)
$pattern = '/#((?:\d{1,3}\.){3}\d{1,3})|((\[[0-9a-fA-F:]+\]))#/';
// 2. 判断目标 IP 是 IPv4 还是 IPv6,以决定替换时是否加括号
$is_ipv6_target = (strpos($target_ip, ':') !== false);
// 准备替换后的格式
$replacement_format = '#%s#';
$final_ip = $target_ip;
// 如果目标包含冒号,视为 IPv6,自动加上方括号以兼容原有格式
if ($is_ipv6_target) {
$final_ip = "[$target_ip]";
}
// 3. 执行替换
// 使用回调函数处理,以确保完美兼容原有的括号风格
$result_content = preg_replace_callback($pattern, function ($matches) use ($final_ip) {
return '#' . $final_ip . '#';
}, $original_content);
}
} else {
// 默认演示文本
$original_content = "nonencrypt/55838#1.1.1.1#55838
nonencrypt/26153#[2403:2c80::1]#26153
server/test#192.168.0.5#8080
link/db#[::1]#3306";
$result_content = $original_content; // 默认结果显示原样
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IP 批量替换工具 (兼容 IPv4/IPv6)</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f6f9;
color: #333;
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
}
.container {
background-color: #fff;
width: 100%;
max-width: 900px;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
color: #2c3e50;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
color: #555;
}
input[type="text"] {
width: 100%;
padding: 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
box-sizing: border-box;
}
.btn-submit {
background-color: #007bff;
color: white;
border: none;
padding: 12px 24px;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
width: 100%;
transition: background-color 0.3s;
}
.btn-submit:hover {
background-color: #0056b3;
}
.grid-layout {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-top: 20px;
}
@media (max-width: 768px) {
.grid-layout {
grid-template-columns: 1fr;
}
}
textarea {
width: 100%;
height: 300px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: monospace;
resize: vertical;
box-sizing: border-box;
background-color: #fafafa;
}
.box-title {
font-size: 14px;
color: #777;
margin-bottom: 5px;
display: flex;
justify-content: space-between;
}
.tag {
background: #e9ecef;
padding: 2px 6px;
border-radius: 3px;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<h1>IP 批量替换工具</h1>
<form method="post" action="">
<div class="form-group">
<label for="target_ip">替换为的新 IP (Fixed IP)</label>
<input type="text" id="target_ip" name="target_ip" placeholder="例如: 192.168.1.1 或 2001:db8::1" required>
</div>
<div class="grid-layout">
<div>
<div class="box-title">
<span>原始内容</span>
<span class="tag">Input</span>
</div>
<textarea name="content" placeholder="在此粘贴包含 #IP# 的文本..."><?php echo htmlspecialchars($original_content); ?></textarea>
</div>
<div>
<div class="box-title">
<span>替换结果</span>
<span class="tag">Output</span>
</div>
<textarea readonly onclick="this.select()"><?php echo htmlspecialchars($result_content); ?></textarea>
</div>
</div>
<div style="margin-top: 20px;">
<button type="submit" class="btn-submit">执行替换</button>
</div>
</form>
</div>
</body>
</html>