if (mb_strlen() != #) 的问题

发布于 2025-01-08 09:30:15 字数 4344 浏览 0 评论 0原文

这可能是真正肮脏和混乱的代码,所以任何输入都会有帮助,但我的主要问题是,如果字符串是正确的字符数(10 或13)。我不确定哪里出了问题。在第 64 行。

请帮忙!谢谢。

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Link Generator</title>
</head>

<body>
    <?php

        function showForm() {

            if (empty($_POST['title'])) {
                $title = "Book Title";
            } else {
                $title = $_POST['title'];
            }
    ?>

    <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">

        Search by ISBN<br />
        <input type="text" maxlength="13" name="ISBN" size="30" value="ISBN" onblur="if(this.value == '') { this.value='ISBN'}" onfocus="if (this.value == 'ISBN') {this.value=''}" /><br />

        <br />OR<br /><br />

        Search by Title <strong>and</strong> Author<br />
        <input type="text" maxlength="13" name="title" size="30" value="<?php echo $title; ?>" onblur="if(this.value == '') { this.value='Book Title'}" onfocus="if (this.value == 'Book Title') {this.value=''}" /><br />
        <input type="text" maxlength="13" name="aname" size="30" value="Author Name" onblur="if(this.value == '') { this.value='Author Name'}" onfocus="if (this.value == 'Author Name') {this.value=''}" /><br />

        <br /><input type="submit" name="submit" value="Generate Link" />

    </form>

    <?php

        }

        function generateLink_ISBN() {
            echo "Your link has been generated:<br />";
            echo "http://xxx.com/uhtbin/cgisirsi.exe/x/0/0/5?search_type=search&searchdata1=" . $_POST['ISBN'] . "&library=ALL&sort_by=PBYR";   
        }

        function generateLink_title() {
            echo "Your link has been generated:<br />";
            echo "http://xxx.com/uhtbin/cgisirsi.exe/x/0/0/5?search_type=search&searchdata1=" . $_POST['title'] . "+" . $_POST['aname'] . "&library=ALL&sort_by=PBYR";  
        }

        if(isset($_POST['submit'])) {

            if (isset($_POST['ISBN']) && isset($_POST['title']) && isset($_POST['aname']) && ($_POST['ISBN'] == 'ISBN') && ($_POST['aname'] == 'Author Name') && ($_POST['title'] == 'Book Title')) {

                echo "<h1>You did not input any information</h1>";
                showForm();

            } elseif (isset($_POST['ISBN']) && ($_POST['ISBN'] != 'ISBN')) {

                if (isset($_POST['ISBN']) && ($_POST['ISBN'] != 'ISBN') && (!is_numeric ($_POST['ISBN']))) {

                     echo "<h1>The ISBN you entered did not contain all numerics</h1>";
                     showForm();

                } elseif (isset($_POST['ISBN']) && ($_POST['ISBN'] != 'ISBN') && ((mb_strlen($_POST['ISBN'], 'utf-8') != 13) | (mb_strlen($_POST['ISBN'], 'utf-8') != 10))) {

                    echo "<h1>The ISBN you entered was too long or too short. ISBN's are 10 or 13 numbers in length.</h1>";
                    showForm();

                } else  {

                    generateLink_ISBN();

                }

            } elseif (isset($_POST['title']) && isset($_POST['aname']) && ($_POST['aname'] != 'Author Name') | ($_POST['title'] != 'Book Title')) {

                if (isset($_POST['title']) && isset($_POST['aname']) && ($_POST['title'] == 'Book Title') && ($_POST['aname'] != 'Author Name')) {

                    echo "<h1>To search by author's name, you must also include the book title.</h1>";
                    showForm();

                } elseif (isset($_POST['title']) && isset($_POST['aname']) && ($_POST['aname'] == 'Author Name') && ($_POST['title'] != 'Book Title')) {

                    echo "<h1>To search by book title, you must also include the author's name.</h1>";
                    showForm();

                } else {

                    generateLink_title();

                }

            } else {

                showForm(); 

            }

        } else {

            showForm(); 

        }

    ?>

</body>

This is probably really dirty and messy code, so any input on that would be helpful as well, but my main issue is that I can't get the "ISBN" input to process if the string is the correct number of characters (10 or 13). I'm not sure where it is going wrong. It's on line 64.

Please help! Thank you.

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Link Generator</title>
</head>

<body>
    <?php

        function showForm() {

            if (empty($_POST['title'])) {
                $title = "Book Title";
            } else {
                $title = $_POST['title'];
            }
    ?>

    <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">

        Search by ISBN<br />
        <input type="text" maxlength="13" name="ISBN" size="30" value="ISBN" onblur="if(this.value == '') { this.value='ISBN'}" onfocus="if (this.value == 'ISBN') {this.value=''}" /><br />

        <br />OR<br /><br />

        Search by Title <strong>and</strong> Author<br />
        <input type="text" maxlength="13" name="title" size="30" value="<?php echo $title; ?>" onblur="if(this.value == '') { this.value='Book Title'}" onfocus="if (this.value == 'Book Title') {this.value=''}" /><br />
        <input type="text" maxlength="13" name="aname" size="30" value="Author Name" onblur="if(this.value == '') { this.value='Author Name'}" onfocus="if (this.value == 'Author Name') {this.value=''}" /><br />

        <br /><input type="submit" name="submit" value="Generate Link" />

    </form>

    <?php

        }

        function generateLink_ISBN() {
            echo "Your link has been generated:<br />";
            echo "http://xxx.com/uhtbin/cgisirsi.exe/x/0/0/5?search_type=search&searchdata1=" . $_POST['ISBN'] . "&library=ALL&sort_by=PBYR";   
        }

        function generateLink_title() {
            echo "Your link has been generated:<br />";
            echo "http://xxx.com/uhtbin/cgisirsi.exe/x/0/0/5?search_type=search&searchdata1=" . $_POST['title'] . "+" . $_POST['aname'] . "&library=ALL&sort_by=PBYR";  
        }

        if(isset($_POST['submit'])) {

            if (isset($_POST['ISBN']) && isset($_POST['title']) && isset($_POST['aname']) && ($_POST['ISBN'] == 'ISBN') && ($_POST['aname'] == 'Author Name') && ($_POST['title'] == 'Book Title')) {

                echo "<h1>You did not input any information</h1>";
                showForm();

            } elseif (isset($_POST['ISBN']) && ($_POST['ISBN'] != 'ISBN')) {

                if (isset($_POST['ISBN']) && ($_POST['ISBN'] != 'ISBN') && (!is_numeric ($_POST['ISBN']))) {

                     echo "<h1>The ISBN you entered did not contain all numerics</h1>";
                     showForm();

                } elseif (isset($_POST['ISBN']) && ($_POST['ISBN'] != 'ISBN') && ((mb_strlen($_POST['ISBN'], 'utf-8') != 13) | (mb_strlen($_POST['ISBN'], 'utf-8') != 10))) {

                    echo "<h1>The ISBN you entered was too long or too short. ISBN's are 10 or 13 numbers in length.</h1>";
                    showForm();

                } else  {

                    generateLink_ISBN();

                }

            } elseif (isset($_POST['title']) && isset($_POST['aname']) && ($_POST['aname'] != 'Author Name') | ($_POST['title'] != 'Book Title')) {

                if (isset($_POST['title']) && isset($_POST['aname']) && ($_POST['title'] == 'Book Title') && ($_POST['aname'] != 'Author Name')) {

                    echo "<h1>To search by author's name, you must also include the book title.</h1>";
                    showForm();

                } elseif (isset($_POST['title']) && isset($_POST['aname']) && ($_POST['aname'] == 'Author Name') && ($_POST['title'] != 'Book Title')) {

                    echo "<h1>To search by book title, you must also include the author's name.</h1>";
                    showForm();

                } else {

                    generateLink_title();

                }

            } else {

                showForm(); 

            }

        } else {

            showForm(); 

        }

    ?>

</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2025-01-15 09:30:15

这总是评估为 true:

((mb_strlen($_POST['ISBN'], 'utf-8') != 13) | (mb_strlen($_POST['ISBN'], 'utf-8') != 10))

您说的是“如果 ISBN 不是 13 个字符长或 ISBN 不是 10 个字符长,则为 true”。但任何字符串都不能同时包含 13 个字符和 10 个字符。

试试这个:

!((mb_strlen($_POST['ISBN'], 'utf-8') == 13) | (mb_strlen($_POST['ISBN'], 'utf-8') == 10))

这将是“如果 ISBN 不是 13 个字符或 ISBN 不是 10 个字符,则为 true”。

This always evaluates to true:

((mb_strlen($_POST['ISBN'], 'utf-8') != 13) | (mb_strlen($_POST['ISBN'], 'utf-8') != 10))

You're saying "if ISBN isn't 13 characters long or ISBN isn't 10 characters long, then true". But no string can be both 13 characters and 10 characters.

Try this instead:

!((mb_strlen($_POST['ISBN'], 'utf-8') == 13) | (mb_strlen($_POST['ISBN'], 'utf-8') == 10))

Which would be "if it is not the case that either ISBN is 13 characters or ISBN is 10 characters, then true".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文