PHP number of occurrences of line characters in another line

Say I have two lines.

$needle = 'AGUXYZ';
$haystack = 'Agriculture ID XYZ-A';

I want to calculate how often characters found in $needle, in $haystack. The $haystackcharacters "A" (twice), "X", "Y" and "Z" are present, all of which are in the needle, so the result should be 5 (case sensitive).

Is there any function for this in PHP or should I program it myself?

Thanks in advance!

+5
source share
9 answers

You can calculate the length of the original string and the length of the string without these characters. The differences between them are the number of matches.

Basically,

$needle = 'AGUXYZ';
$haystack = 'Agriculture ID XYZ-A';

, . .

$count = strlen($haystack) - strlen(str_replace(str_split($needle), '', $haystack));

: . - $needle. $needle .

, $needle , , str_split. str_replace. $search .

,

echo "Count = $count\n";

:

Count = 5

+18

;

function count_occurences($char_string, $haystack, $case_sensitive = true){
    if($case_sensitive === false){
        $char_string = strtolower($char_string);
        $haystack = strtolower($haystack);
    }

    $characters = str_split($char_string);
    $character_count = 0;
    foreach($characters as $character){
        $character_count = $character_count + substr_count($haystack, $character);
    }
    return $character_count;
}

;

$needle = 'AGUXYZ';
$haystack = 'Agriculture ID XYZ-A';
print count_occurences($needle, $haystack);

false, .

+6

, , substr_count :

<?php
    $sourceCharacters = str_split('AGUXYZ');
    $targetString = 'Agriculture ID XYZ-A';
    $occurrenceCount = array();

    foreach($sourceCharacters as $currentCharacter) {
        $occurrenceCount[$currentCharacter] = substr_count($targetString, $currentCharacter);
    }

    print_r($occurrenceCount);
?>
+4

, :

$count = substr_count($haystack , $needle);

edit: substr_count. $needle ( @Alan Whitelaw)

+3

, Regex

echo preg_match_all("/[$needle]/", $haystack, $matches);

( ) FALSE, . @thai, .


- , count_chars:

$needle = 'AGUXYZ';
$haystack = 'Agriculture ID XYZ-A';

$occurences = array_intersect_key(
    count_chars($haystack, 1),
    array_flip(
        array_map('ord', str_split($needle))
    )
);

, ASCII .

foreach($occurences as $char => $amount) {
    printf("There is %d occurences of %s\n", $amount, chr($char));
}

$occurences array_sum, .

+1

substr_count . . , $needle .

0

PHP substr_count, . :

function substr_multi_count ($haystack, $needle, $offset = 0, $length = null) {
    $ret = 0;

    if ($length === null) {
        $length = strlen($haystack) - $offset;
    }

    for ($i = strlen($needle); $i--; ) {
        $ret += substr_count($haystack, $needle, $offset, $length);
    }

    return $ret;
}
0

I will do something like: divide the string into characters ( str_split), and then use array_count_valuesto get an array of each character, how many times this happens.

    $needle = 'AGUXYZ';
        $string = "asdasdadas asdadas asd asdsd";
        $array_chars = str_split($string);
        $value_count = array_count_values($array_chars);
        for($i=0;$i<count($needle);$i++)
           echo $needle[$i]. " is occur " . 
          ($value_count[$needle[$i]] ? $value_count[$needle[$i]] : '0')." times";
0
source

I have a recursive method to overcome this:

function countChar($str){

  if(strlen($str) == 0) return 0;

  if(substr($str,-1) == "x") return 1 + countChar(substr($str,0,-1));

  return 0 + countChar(substr($str,0,-1));

}

  echo countChar("xxSR"); // 2
  echo countChar("SR"); // 0
  echo countChar("xrxrpxxx"); // 5
0
source

All Articles