繁体   English   中英

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

[英]Issue with if (mb_strlen() != #)

这可能真的很脏而且凌乱,所以任何输入也将有所帮助,但是我的主要问题是,如果字符串是正确的字符数(10或10,则不能获得“ ISBN”输入) 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>

这总是评估为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”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM