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 Postkept 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 PostWell 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 PostWhy 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 PostAgree on the latter.
In the first, the
\b
within 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@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,194
¯\_(ツ)_/¯
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
diafol
pritaeas
2,194
¯\_(ツ)_/¯
Moderator
Featured Poster
pritaeas
2,194
¯\_(ツ)_/¯
Moderator
Featured Poster
diafol
pritaeas
2,194
¯\_(ツ)_/¯
Moderator
Featured Poster
diafol
commented:
thanks for the explanation
+14
diafol
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.