/    Sign up×
Community /Pin to ProfileBookmark

Counting apperence of word in TXT file?

Anyone know a way to make script, that will load a big TXT file and count how many times some word appears in it? Example, I wish to count how many “Name:” words are there in in this file I loaded..

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@pyroOct 17.2003 — Try something like this:

[code=php]<?PHP
$str = "This is a test string which will contain test characters. Let's run a test, and then be done.";
preg_match_all("/btestb/", $str, $num);
$num = count($num[0]);
echo $num
?>[/code]
Copy linkTweet thisAlerts:
@ExtremeauthorOct 17.2003 — I don't see code to browse for my text file?? I have to load it into script somehow..
Copy linkTweet thisAlerts:
@pyroOct 17.2003 — I just posted the script as an example, not a full script...

[code=php]
<?PHP
$contents = file("yourfile.txt");
$lines = "";
foreach ($contents as $line) {
$lines .= $line;
}
preg_match_all("/btestb/", $lines, $num);
$num = count($num[0]);
echo $num
?>[/code]
×

Success!

Help @Extreme spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...