Loading, please wait...

A to Z Full Forms and Acronyms

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

Feb 28, 2022 PHP, 1911 Views
How to demonstrate the regular expression to count the occurrence of a 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);
?>
A to Z Full Forms and Acronyms