PHP

How to demonstrate the regular expression to count the occurrence of given substring in PHP | PHP program

How to demonstrate the regular expression to count the occurrence of given substring in PHP | PHP program

How to demonstrate the regular expression to count the occurrence of given substring in PHP | PHP program

We will demonstrate the regular expression to count the total number of occurrences of a specified substring within the string using the preg_match_all() function.

<?php



$str = "India is a country located in asia";
$pattern = "/in/i";

$count = preg_match_all($pattern, $str);

printf("Total occurrences of are: %d", $count);
?>
TutorialsLink Curator

This is an official account managed by the team TutorialsLink Community