具有空格的字符串匹配

时间:2019-05-20 08:13:35

标签: php

我正在从Mysql中获取数据,并将结果与​​特定字符串匹配。 字符串1:“ Hello World” 字符串2:“ Hello-World”

Mysql结果将包含具有字符串1和字符串2的句子。我使用了以下代码。

我尝试了strpos($ result_test,$ string_1)!== false。这不会提供任何结果。

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "testing";
    $conn = new mysqli($servername, $username, $password, $dbname);
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT string FROM table";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while ($row = $result->fetch_assoc()) {
            $result_test = $row["string"];

            $string_1= "Hello World";
            $string_2= "Hello-World";


            //case1: Works perfectly
            if ((strpos($result_test , $string_1) !== false)) {
                ?>
                <table style="border: 1px solid">
                    <tr>
                        <td><?php echo $UNV_TCKT_NUM; ?></td>
                        <td><?php echo " - It Exists" ?></td>
                    </tr>
                </table><?php } else {
                ?><table style="border: 1px solid">
                    <tr>
                        <td><?php echo $UNV_TCKT_NUM; ?></td>
                        <td><?php echo " - No" ?></td>
                    </tr>
                </table><?php
            }


    //case2: Doesn't provide correct results due to whitespace
    if((strpos($result_test , $string_2) !== false)) {
                ?>
                <table style="border: 1px solid">
                    <tr>
                        <td><?php echo $UNV_TCKT_NUM; ?></td>
                        <td><?php echo " - It Exists" ?></td>
                    </tr>
                </table><?php } else {
                ?><table style="border: 1px solid">
                    <tr>
                        <td><?php echo $UNV_TCKT_NUM; ?></td>
                        <td><?php echo " - No" ?></td>
                    </tr>
                </table><?php
            }
        }
    }
    ?>

它也应该为“ Hello World”打印“ It exist”。

0 个答案:

没有答案