rjangelov

StringsInPHP - //Problem 3. Find All Matches

Jun 30th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.69 KB | None | 0 0
  1. //Problem 3.    Find All Matches   
  2. //Write a script that finds how many times a substring is contained in a given text (perform case insensitive search).
  3. <!DOCTYPE html>
  4. <html>
  5.     <head>
  6.         <meta charset="UTF-8">
  7.         <title></title>
  8.     </head>
  9.     <body>
  10.         <form method="post" action="FindAllMatches.php">
  11.             <input type="text" name="str1">
  12.             <input type="text" name="str2">
  13.             <input type="submit" value="GO">
  14.         </form>
  15.     </body>
  16. </html>
  17. <?php
  18. $string=(string)$_POST['str1'];
  19. $substring=(string)$_POST['str2'];
  20. $occurrences=substr_count($string, $substring); // 2
  21. echo "The substring \"$substring\" is countained $occurrences times";
  22. ?>
Advertisement
Add Comment
Please, Sign In to add comment