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 に答える