分享一个简单的繁简体转换PHP代码轻松实现繁体中文和简体中文相互转换

老铁铺 网络经验 2024年6月8日 86 0
分享一个简单的繁简体转换PHP代码轻松实现繁体中文和简体中文相互转换

有些时候我们需要用到繁体转换,其实实现这个很简单。分享代码如下:

<?php 
$pageTitle = "繁简体转换,繁体中文转简体中文简体中文转繁体中文";
include('/header.php'); 
?>

<style>
    .container {
        max-width: 1100px;
        margin: 0 auto;
        padding: 20px;
    }

    .form-container {
        width: 100%;
        margin-bottom: 20px;
        text-align: center;
    }

    .form-container form {
        margin-bottom: 10px;
    }

    .result-container {
        width: 100%;
        text-align: center;
        margin-top: 20px;
    }

    .result-container textarea {
        width: 98%;
        height: 100px;
        border-radius: 5px;
        background-color: #f5f5f5;
        border: none;
        padding: 10px;
    }

    .clearfix::after {
        content: "";
        clear: both;
        display: table;
    }

    .form-container form textarea {
        background-color: #f5f5f5;
        width: 98%;
        padding: 10px;
        height: 100px;
        border-radius: 5px;
    }

    .form-container form input[type="submit"],
    .copy-btn {
        background-color: #007bff;
        color: #fff;
        border: none;
        border-radius: 5px;
        padding: 10px 20px;
        cursor: pointer;
        margin-right: 10px;
        width: 120px;
    }

    .button-container {
        margin-top: 15px;
    }

    .button-container button {
        margin-bottom: 10px;
    }

    @media (max-width: 768px) {
        .form-container form input[type="submit"],
        .copy-btn {
            width: 100%;
            margin-right: 0;
        }
    }
</style>

<div class="container">
    <div class="form-container">
        <h2>繁简体转换</h2>
        <form method="post" action="">
            <textarea id="text" name="text" rows="4" cols="50"></textarea><br>
            <input type="radio" id="to_simple" name="conversion_type" value="zh"> 繁体转简体
            <input type="radio" id="to_traditional" name="conversion_type" value="cht"> 简体转繁体<br>
            <div class="button-container">
                <input type="submit" name="submit" value="转换">
                <button type="button" class="copy-btn" onclick="copyOutputText()">复制输出结果</button>
            </div>
        </form>
    </div>
    <div class="result-container">
        <?php
        if(isset($_POST['submit']) && isset($_POST['conversion_type'])) {
            $text = $_POST['text'];
            if(empty($text)) {
                echo "<textarea readonly>输入内容为空,请输入需要转换的内容后再试!</textarea>";
            } else {
                $conversion_type = $_POST['conversion_type'];
                $api_url = "http://api.fanyi.baidu.com/api/trans/vip/translate";
                $app_id = "百度API ID号";
                $app_key = "百度API秘钥";
                $salt = rand();
                $sign = md5($app_id . $text . $salt . $app_key);
                $params = array(
                    'q' => $text,
                    'from' => 'auto',
                    'to' => $conversion_type,
                    'appid' => $app_id,
                    'salt' => $salt,
                    'sign' => $sign
                );
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $api_url);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $response = curl_exec($ch);
                curl_close($ch);
                $result = json_decode($response, true);
                if(isset($result['trans_result'])) {
                    $converted_text = $result['trans_result'][0]['dst'];
                    echo "<textarea readonly>" . $converted_text . "</textarea>";
                } else {
                    echo "<p><strong>转换失败,请重试。</strong></p>";
                }
            }
        } else {
            echo "<textarea readonly></textarea>";
        }
        ?>
    </div>
    <div class="clearfix"></div>
</div>

<script>
    function copyOutputText() {
        var text = document.querySelector('.result-container textarea').value;
        var input = document.createElement('textarea');
        document.body.appendChild(input);
        input.value = text;
        input.select();
        document.execCommand('copy');
        document.body.removeChild(input);
        alert("已复制输出结果到剪贴板!");
    }
</script>

<?php include('footer.php'); ?>

代码案例如下链接: https://2fa.hk/tools/compare/index.php

上一篇: 最新免费可用的新加坡苹果ID账号分享可下载免费APP
下一篇: 如何使用 TDATA 登录 Telegram 账户?

评论

精彩评论

评论 (0)

pc广告位