Is it pasible to find all occurrences of strings "North" and "North-East" in some txt file and save number of occurrences in some variable??
Drugsxxx 0 Newbie Poster
Recommended Answers
Jump to Post— KamranArshad 0kept thing simple
first get content of file through "file_get_contents"
like {$text = file_get_contents("./folder/file.txt", true);}
now you have array of file content in $text variable
now use {echo substr_count($my_text ,"North"); }
this will print number of occurrence of substring in file.
Hopefully this will …
Jump to Post— KamranArshad 0Well i think you need complete code for this
$getText = file_get_contents("text.txt", true); $North = substr_count($getText ,"North"); $North_East = substr_count($getText ,"North-East"); echo "North =". $North; echo "<br>"; echo "North_East =". $North_East ;This is working example same as you write txt file data
Jump to Post— KamranArshad 0Why don't you subtract "North-East" from "North" You get the total number
$getText = file_get_contents("text.txt", true); $North = substr_count($getText ,"North"); $North_East = substr_count($getText ,"North-East"); $getNorth = $North - $North_East ; echo "North =". $getNorth; echo "<br>"; echo "North_East =". $North_East ;
Jump to Post— pritaeas 2,276Agree on the latter.
In the first, the
\bwithin the brackets means backspace, and the pipe does not mean 'or' (just the pipe char), so it matches'North 'twice because of the space. Try it with North at the end of the string ;)
Jump to Post— Member #120589@P - thanks for the explanation - it looked all wrong - so decided on the array_count_values solution.
All 17 Replies
KamranArshad 0 Light Poster
Drugsxxx 0 Newbie Poster
KamranArshad 0 Light Poster
pritaeas 2,276 Most Valuable Poster Moderator Featured Poster
Drugsxxx 0 Newbie Poster
KamranArshad 0 Light Poster
Drugsxxx 0 Newbie Poster
KamranArshad 0 Light Poster
Drugsxxx 0 Newbie Poster
KamranArshad 0 Light Poster
Member #120589
pritaeas 2,276 Most Valuable Poster Moderator Featured Poster
pritaeas 2,276 Most Valuable Poster Moderator Featured Poster
Member #120589
pritaeas 2,276 Most Valuable Poster Moderator Featured Poster
Member #120589 commented: thanks for the explanation +14
Member #120589
Drugsxxx 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.