sub count_occurrences{ my($part, $whole, $cs)=@_; # $part -- the text you're searching for # $whole - the string within you're searching # $cs -- case sensitive? 1=yes, 0=no (may be omitted for non cs search) my($count)=0; my($null)="\0"; if($cs==1){ while($whole=~/$part/s){ $count++; $whole=~s/$part/$null/; } }else{ while($whole=~/$part/is){ $count++; $whole=~s/$part/$null/i; } } return $count; }