2

Trying to take numbers from a text file and see how many times they occur. I've gotten to the point where I can print all of them out, but I want to display just the number once, and then maybe the occurrences after them (ie: Key | Amount; 317 | 42). Not looking for an Answer per se, all learning is good, but if you figure one out for me, that would be awesome as well!

2
  • Show us what you've got so far, and we can offer better advice. Commented Mar 22, 2011 at 18:59
  • $numbers=file_get_contents('names.txt'); $convert = explode(" ", $numbers); for ($i=0;$i<count($convert);$i++) { //sort values in array asc. echo $convert[$i]."<br />"; } so far, it just lists the numbers to the screen for my convenience. Commented Mar 24, 2011 at 12:09

3 Answers 3

1

preg_match_all will return the number of matches against a string.

$count = preg_match_all("#$key#", $string);
print "{$key} - {$count}";
Sign up to request clarification or add additional context in comments.

4 Comments

In order to use preg_match_all here, you'd first have to get a list of all the unique words in the file. While you're in the process of doing that you can easily count their occurrences. Running preg_match_all after you've gotten the list would just be wasteful.
@itchy Am I misreading the question? He says he's trying to match a string thats taken from a text file. Why would you count unique words?
"Trying to take numbers from a text file and see how many times they occur." Meaning there is a text file containing numbers, some of which are repeated, and he has to extract all of those numbers and count how many times they each occur. I can't think of any other meaning for that, though I could be misreading it too.
the numbers are random and unknown, so thats where it gets a little weird. i could understand using RegEx and such, but i don't know the numbers which i am working with.
1

So if you're already extracting the data you need, you can do this using a (fairly) simple array:

$counts = array();

foreach ($keysFoundFromFile AS $key) {
    if (!$counts[$key]) $counts[$key] = 0;
    $counts[$key]++;
}

print_r($counts);

If you're already looping to extract the keys from the file, then you can simply assign them directly to the $counts array without making a second loop.

Comments

1

I think you're looking for the function substr_count().
Just a heads up though, if you're looking for "123" and it finds "4512367" it will match it as part of it. The alternative would be using RegEx and using word boundaries:

$count = preg_match_all('|\b'. preg_quote($num) .'\b|', $text);

(preg_quote() for good practice, \b for word boundaries so we can be assured that it's not a number embedded in another number.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.