Open In App

How to check a string starts/ends with a specific string in PHP ?

Last Updated : 24 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to check that a given string is starting or ending with a given specific string in PHP.

We require the use of XAMPP, Netbeans to run this code.

Input: "Geeks For Geeks"
Starting with: "G"
Output: "Geeks For Geeks Starts with String G"

In PHP version 8+, there are str_starts_with(), str_ends_with() functions which take two arguments one as, original string, the second as a string to be checked. 

We will learn, how to check string starts/ends with a specific string.

 

Approach:

  • We will create a basic HTML form to input two strings, the main string, and the string to be checked.
  • We will validate the inputs using the str_starts_with(), str_ends_with() functions.

PHP code: Create a file “index.php” and write the following PHP code.

PHP




<!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 = "The Given string [$_POST[mainString]] 
                    Starts with [$_POST[checkString]]";
        } else {
            $msg = "The Given string [$_POST[mainString]] 
                  do not Starts with [$_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 = "The Given string [$_POST[mainString]] 
                    ends with [$_POST[checkString]]";
        } else
        {
            $msg = "The Given string [$_POST[mainString]] 
                    do not ends with [$_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>


Input: 

Input: Geeks For Geeks
String To be Checked: Geek
Type: Starts With
 
Input: Geeks For Geeks
String To be Checked: s
Type: Ends With
 
Input: Geeks For Geeks
String To be Checked: For
Type: Starts With

Output: 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads