Bagaimana cara memeriksa string yang dimulai/diakhiri dengan string tertentu di PHP?

0
(0)

<!DOCTYPE html>

<?php

$msg = "";

error_reporting(0);

  

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

      

    if ($_POST['stringSearchType'] == 0)

    {

        if ((str_starts_with($_POST['mainString'],

                strtoupper($_POST['checkString'])))

            || (str_starts_with($_POST['mainString'],

                strtolower($_POST['checkString'])))

            || (str_starts_with($_POST['mainString'],

                $_POST['checkString'])))

        {

            $msg = "String yang diberikan [$_POST[mainString]]

                    Dimulai dengan [$_POST[checkString]]";

        } else {

            $msg = "String yang diberikan [$_POST[mainString]]

                  do tidak Dimulai dengan [$_POST[checkString]]";

        }

    } else if ($_POST['stringSearchType']) {

        if ((str_ends_with($_POST['mainString'],

                strtoupper($_POST['checkString'])))

            || (str_ends_with($_POST['mainString'],

                strtolower($_POST['checkString'])))

            ||  (str_ends_with($_POST['mainString'],

                $_POST['checkString'])))

        {

            $msg = "String yang diberikan [$_POST[mainString]]

                    berakhir dengan [$_POST[checkString]]";

        } else

        {

            $msg = "String yang diberikan [$_POST[mainString]]

                    do tidak berakhir dengan [$_POST[checkString]]";

        }

    }

}

              

?>

<html>

 

<body>

    <form action="index.php" method="POST">

        <p>Input Main String</p>

 

        <input type="text" autocomplete="off" name="mainString"

            placeholder="Eg. Geeks for Geeks" required="true">

        <p>Input String to be Checked</p>

 

        <input type="text" autocomplete="off" name="checkString"

            placeholder="Eg. Geek" required="true"><br>

        <select name="stringSearchType">

            <option value=0>Starts With</option>

            <option value=1>Ends With</option>

        </select><br>

        <hr>

        <input type="submit" name="submit">

    </form>

    <h2 style="color:green">

        <?php

            if ($msg) {

                echo $msg;

            }          

        ?>

    </h2>

</body>

 

</html>

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.