PHP program to find position of a given character

Write a PHP program to find a position of a given character into given string


In this tutorial, you will learn how to find a position of a given character into given string in php.

Below is the php script to find a position of a given character into given string in php.

<html>
<head>
    <title>PHP program to find position of a given character</title>
</head>
<body>

<h2>Write a PHP program to find position of a given character in php</h2>

    <form action="" method="POST">
        <input type="text" name="str" placeholder="enter the string here"/></br>
        <input type="text" name="chr" placeholder="enter the char here"/></br>
        <input type="submit"/></br>
    </form>

    <?php
        if($_POST)
        {
            $str = $_POST['str'];
            $chr = $_POST['chr'];

            if(strpos($str,$chr) === false)
            {
                echo "char not found!!";
            }
            else
            {  
                $pos = strpos($str,$chr);
                echo "$pos is the position of $chr in $str";
            }
        }
    ?>

</body>
</html>


Thanks for reading.

© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT