PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/substr_count.php

https://github.com/dim2k2006/php-homework
PHP | 30 lines | 23 code | 6 blank | 1 comment | 5 complexity | 08129427bcd4f80b1b1545c5baac9984 MD5 | raw file
  1. <?php
  2. $text = 'This is a test';
  3. echo substr_count($text, 'is', 3, 5); // 2
  4. echo "<br>";
  5. //собственная реализация
  6. function my_substr_count ($text, $string, $offset = 0, $length = 0) {
  7. if (($offset+$length) > strlen($text)) {
  8. echo "error!";
  9. exit;
  10. }
  11. if($length == 0) {
  12. $text = $text;
  13. } else {
  14. $tmp = str_split($text);
  15. $text = "";
  16. for ($i = $offset; $i <= ($offset+$length); $i++) {
  17. $text .= $tmp[$i];
  18. }
  19. }
  20. $string = '/'.$string.'/';
  21. preg_match_all($string, $text, $matches, PREG_OFFSET_CAPTURE, 0);
  22. echo '<br>'.count($matches[0]);
  23. }
  24. echo my_substr_count ($text, 'is', 3, 5);