PHP | Найдите количество вхождений подстроки
Опубликовано: 24 Февраля, 2022
Нам даны две строки s1 и s2. Нам нужно найти количество вхождений s2 в s1.
Примеры:
Ввод: $ s1 = "geeksforgeeks", $ s2 = "geeks"
Выход: 2
Пояснение: s2 появляется 2 раза в s1
Ввод: $ s1 = "Привет, Шубхам. Как дела?";
$ s2 = "шубхам"
Выход: 0
Пояснение: Обратите внимание на первую букву s2.
отличается от подстрок в s1.
Рекомендуется: сначала попробуйте свой подход в {IDE}, прежде чем переходить к решению.
The problem can be solved using PHP in built function for counting the number of occurrences of a substring in a given string.The in built function used for the given problem is:
- substr_count(): The substr_count() function counts the number of times a substring occurs in a string.
Note: The substring is case-sensitive.
<?php// PHP program to count number of times// a s2 appears in s1. $s1 = "geeksforgeeks";$s2 = "geeks"; $res = substr_count($s1, $s2); echo($res);?> |
Выход :
2