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

时间:2012-02-23 21:44:35

标签: php conditional

这可能是非常脏和杂乱的代码,所以任何输入都会有帮助,但我的主要问题是,如果字符串是正确的字符数,我无法处理“ISBN”输入(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>

1 个答案:

答案 0 :(得分:2)

总是评估为true:

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

您说的是“如果ISBN长度不是13个字符,或者ISBN长度不是10个字符,则为真”。但是没有字符串可以是13个字符和10个字符。

请改为尝试:

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

如果不是ISBN不是13个字符或ISBN是10个字符,那么这将是真实的。“