Learn How To Use Strstr Function With Example In PHP

admin_img Posted By Bajarangi soft , Posted On 24-09-2020

The strstr() function searches for the first occurrence of a string inside another string. This function returns the rest of the string, or FALSE, if the string to search for is not found. This function is case-sensitive.

PHP strstr Function with example

Syntax of strstr() function and Usage:

strstr(string,search,before_search)


Parameter Values

string          -> Specifies the string to search.

search          -> Specifies the string to search for.
                   If this parameter is a number, it will search for the character matching the ASCII value of the number.

before_search   -> A boolean value whose default is "false". If set to "true",
                   it returns the part of the string before the first occurrence of the search parameter.


Let's learn

1.Create strstr().

strstr("Hello world!,Welcome to bajarangisoft","s");


2.Print string using strstr() function.

echo strstr("Hello world!,Welcome to bajarangisoft","s");


Example(1)

<?php
    echo strstr("Hello world!,Welcome to bajarangisoft","s");
?>


Example(2)

<?php
   echo strstr("Hello world!,Welcome to bajarangisoft","s",true);
?>


Example(3)

<?php
  echo strstr("Hello world!,Welcome to bajarangisoft",111);
?>


Complete Code of strstr() Function:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP strstr function</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>PHP strstr() Function</h1>
    </div>
    <br>
    <h2>The strstr() function searches for the first occurrence of a string inside another string.</h2>
    <div class="well">
        <?php
        echo "<h4>". strstr("Hello world!,Welcome to bajarangisoft","s")."</h4>";
        echo "<h4>". strstr("Hello world!,Welcome to bajarangisoft","s",true)."</h4>";
        echo "<h4>". strstr("Hello world!,Welcome to bajarangisoft",111)."</h4>";
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post