0

我试图在忽略大小写的情况下计算给定单词的出现次数。我试过了

<?php 
 $string = 'Hello World! EARTh in earth and EARth';//string to look into.
  if(stristr($string, 'eartH')) {
    echo 'Found';// this just show eartH was found.
  }
  $timesfound = substr_count($string, stristr($string, 'eartH'));// this count how many times. 

  echo $timesfound; // this outputs 1 instead of 3.
4

2 回答 2

4

搜索前将字符串小写:

$string = 'Hello World! EARTh in earth and EARth';
$search = 'EArtH';
var_dump(substr_count(strtolower($string), strtolower($search))); // outputs 3
于 2012-09-23T21:15:10.750 回答
-1

尝试 substr_count()

     <?php
     $text = 'This is a test';
     echo strlen($text); // 14

    echo substr_count($text, 'is'); // 2

    ?>
于 2012-09-23T21:17:30.897 回答