# How to get the number of occurrences of symbols from one line to another?

PHP began to study relatively recently, and so, it is necessary to implement counting the number of occurrences of one string within another. Was ideas substr_count or count_chars, but realize it and failed.
June 14th 19 at 20:03
June 14th 19 at 20:05
Solution
`echo similar_text('Word 1', 'Word 2'); // 11`
There is a problem with the Cyrillic alphabet, for the coincidence of the Cyrillic alphabet for each symbol gives +2.
In this example, a match 5 characters, the result will be 10, also the same as the spacebar is still +1, for a total in this example, the result will be 11.
``echo similar_text('This is my test', 'This is my ...'); // 11``

The result is the same 11 as the same This is my (after my gap is taken into account), which is 11 characters.
Um, this isn't it. For example,
``````\$a = "Word 1";
\$b = "Word 2";``````

And we need to calculate how many symbols from the second line match the first - nikita.Stracke commented on June 14th 19 at 20:08
updated - Candido84 commented on June 14th 19 at 20:11
Thanks for the help. - nikita.Stracke commented on June 14th 19 at 20:14
June 14th 19 at 20:07
``````\$a = "Word 1";
\$b = "Word 2";
\$i = 0;
foreach(str_split(\$a) as \$charA){
foreach(str_split(\$b) as \$charB){
\$i = (\$charA === \$charB)? \$i++ : \$i;
}
}
print_r(\$i)``````

c Cyrillic can be a problem
Hmm, do I need to record something? Just swears by the line "foreach(\$a as \$charA)" with the error "Invalid argument supplied for foreach()" - nikita.Stracke commented on June 14th 19 at 20:10
Yes, str_split(\$a) in force but Unicode Dachshund will be released - Candido84 commented on June 14th 19 at 20:13

Find more questions by tags PHP