在PHP表单中键入时自动将孟加拉语数字转换为英语数字

ewm0tg9j  于 2023-01-19  发布在  PHP
关注(0)|答案(1)|浏览(133)

在输入表单中,我如何才能使一个输入框..当用户尝试输入孟加拉数字,它会自动转换它为英语数字
假设输入框如下所示:
<input type="text" name="number" size="45"/>**例如:**如果用户在输入框中输入"""""""""""""""""""""""""""""""""""""""""""""""如果用户在输入框中输入""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
我尝试了很多时间来修复,但我不能做到这一点。我想社会各界会帮助我。提前感谢!!!

taor4pac

taor4pac1#

如果用户在绑定时需要这个,那么你需要一个JavaScript解决方案。如果你在后台需要这个,那么使用unicode对PHP来说有点复杂,因为很多内置函数并不是为使用unicode而设计的。所以你需要使用mb_* 函数,并且需要启用mbstring扩展。可能有一些方法可以在PHP端不使用的情况下实现这一点。我建议只在JavaScript中做这件事。你需要跳过大量的环来让PHP端工作是不值得的。但是我已经附上了前端和后端的解决方案,所以你可以选择你需要的。你也可以结合这些解决方案,如如果用户禁用JavaScript,或旧浏览器等。

<!doctype html>
<html lang="en">
<head>
    <script>
        function range(start, stop) {
            let result = [];
            for (let idx = start.charCodeAt(0), end = stop.charCodeAt(0); idx <= end; idx++) {
                result.push(String.fromCharCode(idx));
            }
            return result;
        }
        function replaceBengaliNumeralsToArabicNumerals(str) {
            const zero = "\u09E6";
            const nine = "\u09EF";
            const chars = range(zero, nine).join('');
            return str.replace(/[\u09E6-\u09EF]/g, d => chars.indexOf(d));
        }
    </script>
</head>
<body>
<!-- blank action in HTML5 means submits to self -->
<form method="POST">
<p><input type="text" name="number" size="45"></p>
<p>Copy from here into the text box: &#x9E6;&#x9E7;&#x9E8;&#x9E9;&#x9EA;&#x9EB;&#x9EC;&#x9ED;&#x9EE;&#x9EF;</p>
<script>
    const el = document.querySelector('input[name=number]');
    ['keyup','keydown','keypress','change','click','dblclick','mousedown','mouseup','input','paste'].forEach(function(ev){
        el.addEventListener(ev, function(){
            el.value = replaceBengaliNumeralsToArabicNumerals(el.value);
        }, false);
    })
</script>
<p>Here's another text box, but without the JavaScript events so you can do this on the backend</p>
<p><input type="text" name="number2" size="45"></p>

<p style="margin-top:50px"><button type="submit">Submit</button></p>
</form>

<?php
if(!empty($_POST)) {

    // using a switch, so we can "break" without invoking "exit".
    // Invoking exit is bad since it affects code flow and prevents you from running stuff afterwards.
    // Another approach is to put this into a function and use return etc.
    switch(true) {
        default:
            // Validation and security check to prevent errors with unexpected input types
            if (!isset($_POST['number'])) {
                echo 'Missing name=number field.';
                break;
            }
            if (!isset($_POST['number2'])) {
                echo 'Missing name=number2 field.';
                break;
            }
            // security check to prevent errors with unexpected input types
            if (is_array($_POST['number'])) {
                echo 'Unexpected array field: name=number.';
                break;
            }
            if (is_array($_POST['number2'])) {
                echo 'Unexpected array field: name=number2.';
                break;
            }

            if ($_POST['number'] === '' && $_POST['number2'] === '') {
                echo 'Blank input was given. Please enter something into one of the text boxes';
                break;
            }

            $n = $_POST['number'];
            $n2 = $_POST['number2'];
            echo "<pre>\n";
            echo "Original Input:\n";
            echo "first field: " . htmlentities($n) . "\n";
            echo "second field: " . htmlentities($n2) . "\n";
            echo "\n";
            echo "After filtering in PHP:\n";
            // credit for strtr_utf8: https://stackoverflow.com/questions/1454401/how-do-i-do-a-strtr-on-utf-8-in-php
            function strtr_utf8($str, $from, $to) {
                $keys = array();
                $values = array();
                preg_match_all('/./u', $from, $keys);
                preg_match_all('/./u', $to, $values);
                $mapping = array_combine($keys[0], $values[0]);
                return strtr($str, $mapping);
            }
            // credit for mb_range: https://gist.github.com/rodneyrehm/1306118
            mb_internal_encoding('UTF-8');
            function mb_range($start, $end) {
                // if start and end are the same, well, there's nothing to do
                if ($start == $end) {
                    return array($start);
                }

                $_result = array();
                // get unicodes of start and end
                list(, $_start, $_end) = unpack("N*", mb_convert_encoding($start . $end, "UTF-32BE", "UTF-8"));
                // determine movement direction
                $_offset = $_start < $_end ? 1 : -1;
                $_current = $_start;
                while ($_current != $_end) {
                    $_result[] = mb_convert_encoding(pack("N*", $_current), "UTF-8", "UTF-32BE");
                    $_current += $_offset;
                }
                $_result[] = $end;
                return $_result;
            }
            function replaceBengaliNumeralsToArabicNumerals($str)
            {
                return strtr_utf8($str, implode('',mb_range("\u{09E6}", "\u{09EF}")), implode('',range(0,9)));
            }

            $replaced_n = replaceBengaliNumeralsToArabicNumerals($n);
            $replaced_n2 = replaceBengaliNumeralsToArabicNumerals($n2);
            echo "first field:" . htmlentities($replaced_n) . "\n";
            echo "second field:" . htmlentities($replaced_n2) . "\n";
            echo "<pre>";
    }
}

?>
</body>
</html>

我想我们也可以包括东方阿拉伯数字:
x一个一个一个一个x一个一个二个x

相关问题