186. PHP Program to search a word existing in given string or not.
How does this program work?
- In this program we are going to learn about how to find whether the word existing in given string or not using PHP.
- In this program stripos() function is used to check whether the given word exists in string or not in PHP.
Here is the code
<html>
<head>
<title>PHP Program To find whether the word existing in given string or not</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="name1" value="" placeholder="Enter a String"/> </td>
</tr>
<tr>
<td> <input type="text" name="name2" value="" placeholder="Enter search word"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit'])) {
$str = $_POST['name1'];
$name = $_POST['name2'];
$pos = stripos($str,$name);
echo "Given string : ".$str."</br>";
// To check given word is existing in string or not
if ($pos !== false)
{
echo $name . " is exist in given string ";
}
else
{
echo $name . " is not exist in given string ";
}
return 0;
}
?>
</body>
</html>