Anybody can tell me about how to find the number of occurrence of substring in the given string without using the string function . for example the string is "when the men get the hen" and the result i expected is the substring 'he' occur 4 times in the given string.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
substr_count('when the men get the hen', 'he');
http://php.net/manual/en/function.substr-count.php
回答2:
Without using any string functions?
Is there any real point in restricting yourself to a set of functions that excludes those specially designed to do what you're trying to do?
$string = 'when the men get the hen';
$substring = 'he';
$cArr = explode($substring,$string);
$substring_count = count($cArr) - 1;
回答3:
Step through the string one character at a time
Check if the current character is an 'h'
If it is check if the next character is an 'e'
If it is an 'e', increase your counter by 1
Go back to step 1