/    Sign up×
Community /Pin to ProfileBookmark

Work with read/write file

hi i have problem in read/write file:

i need some help:
i have 2 file, 1- result.txt and 2-strings.txt

string is like this

>

I just moved to Germany two months ago and bought an 07 MDX from another military member. It has everything I could want. We just returned from a week driving through the Alps and this SUV is simply amazing. Granted, I get to drive it much faster than I could in the states,

then i need to get words from string.txt then put them on results.txt
so problem is not get words…i have words in array,,,
i put every word in one line of result.txt
but i have to add time of appearing(how many time this word repeated) of every word in this line,,,
if i result.txt if word exist i should update that line with add 1 to time of appering and if not exist i should add this word to one line of this result.txt}

result.txt is some thing like this:

“`
[CODE]yes 2
i 3
Germany 1
returned 2[/CODE]

“`

i wrote below code:

“`
[code=php] while (!feof($file_handle2)) {
$line2=fgets($file_handle2);
$exist=0;
if (strpos($line2, $word ) !== FALSE){
global $exist;
$exist=1;
break;
$words = explode(” “, $line2);
$words[2]++;
fwrite($file_handle2, $word.” “.$words[2].”n”);

}
//when all of the file searched
}if ($exist==0) {

fwrite($file_handle2,$word.” “.1.”.”n”);

}
[/code]
“`

but every time i wana add one line, just first line overwriting,,,i wana add new line when word is not exist before in result.txt , and edit current line when word exist before in result.txt
… i know my code is wrong but how should edit or override this?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJun 08.2016 — Not sure what your code is doing, but have you thought of designing your array using the word found as the key and the array value would be the count of the times you found that word.

$ar['lucky'] = 3;

$ar['this'] = 4;

$ar['Germany'] = 12;

Please move that global definition out of your loop. Rather silly there.

And when you explode the line in to words what is 'exist' telling you? You keep turning it on and off, and then you increment the second element of your exploded array, destroying whatever word was in there.

Not sure if I am seeing you correctly but how about this:
[code=php]

while......
{
$words = explode(' ',$line);
foreach ($words as $w)
{
$w = strtolower($w);
if (isset($array["$w"])
$array["$w"]++;
else
$array["$w"] = 1;
}
}
[/code]


At the end the $array will have all the found words with each element having the count of them.
Copy linkTweet thisAlerts:
@NogDogJun 08.2016 — Note that [url=http://php.net/manual/en/function.array-count-values.php]array_count_values()[/url] can do that for you.
[code=php]
$wordCountArray = array_count_values($wordArray);
foreach($wordCountArray as $word => $count)
{
// output $word and $count as desired
}
[/code]
×

Success!

Help @labour 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,
)...